Skip to content

Encode argument values unambiguously in @cache keys - #2808

Open
sk4rp wants to merge 1 commit into
nuwave:masterfrom
sk4rp:fix-cache-key-boolean-null-args
Open

sk4rp wants to merge 1 commit into
nuwave:masterfrom
sk4rp:fix-cache-key-boolean-null-args

Conversation

@sk4rp

@sk4rp sk4rp commented Oct 24, 2026 •

Copy link
Copy Markdown
  • Added or updated tests
  • Documented user facing changes

Changes

CacheKeyAndTagsGenerator::key() joined argument values with implode(), which stringifies false and null to '' and true to '1'.
As a result, foo(flag: false) and foo(flag: null) shared one cache entry, so @cache could return the result of a different argument value.

All non-object argument values are now JSON encoded, so each value keeps its type in the key: arg:null, arg:false, arg:true, arg:"null", arg:5, arg:[1,2].
Strings are quoted, so they no longer collide with null, booleans, numbers or arrays, and colons inside string values can no longer be confused with the key separator.
Objects are still stringified through __toString() as before.

Breaking changes

None. Keys for integer and array arguments are unchanged.
Cache entries for fields with string, boolean or null arguments get new keys, so they are recomputed once after upgrading.

Changelog

Fixed

  • Encode argument values unambiguously in @cache keys, so null, booleans and strings no longer share cache entries

@sk4rp
sk4rp force-pushed the fix-cache-key-boolean-null-args branch from a55b992 to 6d88fd2 Compare October 24, 2026 16:37
@spawnia
spawnia requested a balanced review from Copilot October 25, 2026 11:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new encoding makes null collide with the literal string "null", allowing queries to receive incorrect cached results.

1 open issue

Review effort: Balanced

Comment thread src/Cache/CacheKeyAndTagsGenerator.php Outdated
Comment on lines 40 to 42
$parts[] = is_array($value) || is_bool($value) || $value === null
? \Safe\json_encode($value)
: $value;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spawnia fixed in b84a06a.
All non-object argument values are now JSON encoded, so strings are quoted (arg:"null") and can no longer collide with null, booleans, numbers or arrays.
Objects are still cast through __toString() as before.
Added regression tests for null vs "null" on the key generator and on a @cache field.

@sk4rp
sk4rp force-pushed the fix-cache-key-boolean-null-args branch from 6d88fd2 to b84a06a Compare October 25, 2026 16:23
@sk4rp sk4rp changed the title Distinguish boolean and null arguments in @cache keys Encode argument values unambiguously in @cache keys Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants