Skip to content

Caching query with % character in value string #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JkappenFlashpoint opened this issue Mar 26, 2025 · 1 comment
Open

Caching query with % character in value string #516

JkappenFlashpoint opened this issue Mar 26, 2025 · 1 comment
Assignees

Comments

@JkappenFlashpoint
Copy link

JkappenFlashpoint commented Mar 26, 2025

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]
@mikebronner
Copy link
Owner

mikebronner commented Mar 26, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants