fix(serializer): read Symfony attributes through their public properties - #8519
Open
Maxcastel wants to merge 2 commits into
Open
fix(serializer): read Symfony attributes through their public properties#8519Maxcastel wants to merge 2 commits into
Maxcastel wants to merge 2 commits into
Conversation
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.
Reading a Symfony serializer attribute went through a
method_exists()guard that preferred the getter and fell back to the public property:The getter is deprecated:
https://github.com/symfony/serializer/blob/7.4/Attribute/Groups.php
symfony/serializer CHANGELOG:
https://github.com/symfony/serializer/blob/8.0/CHANGELOG.md
tests
I also added tests to cover the attribute forwarding and make sure regressions in each individual read are caught. The existing tests did not detect incorrect values being forwarded, which is why the test coverage needed to be strengthened.
The forwarding of those attributes had no test at all. Mutating the loader went unnoticed by every suite in the repository:
PropertyMetadataLoaderTest, theapi-platform/serializercomponent suite, andtests/Functionalall stayed green with this in place:PropertyMetadataLoaderTestonly assertedgroupsand the discriminatordefaultType.maxDepth,serializedName,serializedPath,IgnoreandContextwere never asserted, in this component or anywhere else.A single assertion per attribute is not enough either: replacing the read with the value the fixture happens to carry also survives:
So every attribute is now declared twice with distinct values, and the normalization and denormalization contexts differ from each other so that swapping the two reads fails.
DiscriminatorMapis covered by aDataProviderover two fixtures whose
typeProperty,mappinganddefaultTypeall differ.Each of the 13 reads in the loader was then mutated one at a time, using the fixture's own value as the replacement. All 13 are caught.
method_exists($classMetadata, 'getAttributesMetadata')The fallback is not just unreachable, it could never have worked: the property is private, and the getter is present on both bounds of the supported range:
https://github.com/symfony/serializer/blob/7.4/Mapping/ClassMetadata.php
https://github.com/symfony/serializer/blob/8.1/Mapping/ClassMetadata.php
method_exists(ClassDiscriminatorMapping::class, 'getDefaultType')(testForwardsDiscriminatorDefaultType)Superseded by
testForwardsDiscriminatorMapping, which assertstypeProperty,mappinganddefaultTypeon two fixtures instead ofdefaultTypeon one. Its guard could no longer trigger either, with the Symfony floor at^7.4:getDefaultType()is present and undeprecated on both bounds:https://github.com/symfony/serializer/blob/7.4/Mapping/ClassDiscriminatorMapping.php
https://github.com/symfony/serializer/blob/8.1/Mapping/ClassDiscriminatorMapping.php