[13.x] Fix inconsistent accessor attribute name conversion#59325
Draft
JoshSalway wants to merge 3 commits intolaravel:13.xfrom
Draft
[13.x] Fix inconsistent accessor attribute name conversion#59325JoshSalway wants to merge 3 commits intolaravel:13.xfrom
JoshSalway wants to merge 3 commits intolaravel:13.xfrom
Conversation
|
Thanks for submitting a PR! Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
…ution
When using the Attribute syntax (e.g. foo1Bar(): Attribute), multiple
snake_case forms like "foo_1_bar" and "foo1_bar" both map to the same
camelCase method "foo1Bar". However, only "foo1_bar" was stored in the
mutator cache (from Str::snake('foo1Bar')).
This caused append('foo_1_bar')->toArray() to fail with "Call to
undefined method getFoo1BarAttribute()" because the cache lookup for
"foo_1_bar" missed, and it fell through to the legacy accessor path.
The fix normalizes cache keys by round-tripping through camel->snake
conversion, ensuring that alternate snake_case forms resolve to the
same cache entry.
Fixes laravel#54570
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…gments Verifies that both "foo1_bar" and "foo_1_bar" forms correctly resolve to the same Attribute accessor when used with append() and toArray(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Skip expensive Str::camel/Str::snake normalization for the 99% of attribute keys that contain no digits, since the ambiguity this method resolves (e.g. foo_1_bar vs foo1_bar) only exists when digits are present. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes inconsistent behavior when accessing Attribute-style accessors via alternate snake_case forms (e.g.,
foo_1_barvsfoo1_bar). Both forms correctly resolve to the samefoo1Bar()method when getting values, butappend('foo_1_bar')->toArray()would fail because the mutator cache only stored the canonical formfoo1_bar.Normalizes cache keys by round-tripping through camel→snake conversion so that alternate forms resolve to the same entry.
Performance
Two prior PRs addressing this issue (#54578, #54793) were rejected due to performance concerns --
Str::camel()+Str::snake()runs on every attribute access, which is a hot path.This PR includes a fast-path short-circuit: the normalization is only needed when the attribute key contains a digit (the only case where
foo_1_barvsfoo1_barambiguity exists). For the 99% common case (first_name,email,created_at, etc.), the key is returned unchanged with zero overhead beyond a singlepreg_match('/\d/', $key)check.Test plan
foo1_barandfoo_1_barwork withappend()andtoArray()Fixes #54570