Caching query with % character in value string
Opened this issue · 3 comments
Describe the bug
Bug Description
This issue occurs when using GeneaLabs Laravel Model Caching with an Eloquent query that includes a whereIn condition containing percentage (%) symbols in string values. The error "Missing format specifier at end of string" is triggered by vsprintf in CacheKey.php, where the percentage symbol is mistakenly interpreted as a format specifier.
Possible Cause
The vsprintf function expects valid format specifiers (e.g., %s, %d), but a raw % in the query value is incorrectly processed, leading to a ValueError.
Suggested Fix
Escaping percentage symbols or modifying how vsprintf is used in GeneaLabs Model Caching could prevent this issue.
Eloquent Query
Please provide the complete eloquent query that caused the bug, for example:
CachedModel::query()->whereIn('value', ['10%', '20%'])->get();Stack Trace
[2025-03-26 13:34:55] local.ERROR: Missing format specifier at end of string {"exception":"[object] (ValueError(code: 0): Missing format specifier at end of string at vendor/genealabs/laravel-model-caching/src/CacheKey.php:157)
[stacktrace]
#0 vendor/genealabs/laravel-model-caching/src/CacheKey.php(157): vsprintf('A--10%', Array)
#1 vendor/genealabs/laravel-model-caching/src/CacheKey.php(378): GeneaLabs\\LaravelModelCaching\\CacheKey->getInAndNotInClauses(Array)
#2 vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php(791): GeneaLabs\\LaravelModelCaching\\CacheKey->GeneaLabs\\LaravelModelCaching\\{closure}('-filter_id_=_26...', Array, 1)
#3 vendor/genealabs/laravel-model-caching/src/CacheKey.php(373): Illuminate\\Support\\Collection->reduce(Object(Closure))
#4 vendor/genealabs/laravel-model-caching/src/CacheKey.php(53): GeneaLabs\\LaravelModelCaching\\CacheKey->getWhereClauses()
#5 vendor/genealabs/laravel-model-caching/src/Traits/Caching.php(183): GeneaLabs\\LaravelModelCaching\\CacheKey->make(Array, NULL, '')
#6 vendor/genealabs/laravel-model-caching/src/Traits/Buildable.php(106): GeneaLabs\\LaravelModelCaching\\CachedBuilder->makeCacheKey(Array)
Environment
- PHP: [8.2.0]
- Laravel: [10.48.26]
- Model Caching: [11.0.1]
Thanks for reporting this @JkappenFlashpoint. Any chance you could create a PR with a failing test? That would just help get the ball rolling. Thanks :) Also to note, any fixes will only be applied to the latest version, in this case 12.x. Are you able to install version 12 of Model Caching? If so, does the error still occur there?
Hi @mikebronner I added the failing test. #517 This is still relevant for version 12.x.
@JkappenFlashpoint thank you for taking this on! I'll take a look in the next day or two and try to implement a fix.