fix(metadata): read ApiProperty from trait private properties inherited via parent class#8275
Merged
Merged
Conversation
…ed via parent class PHP reflection does not expose private properties declared in a trait used by a parent class (e.g. a Doctrine STI abstract parent) on the child's own ReflectionClass, so AttributePropertyMetadataFactory never read the #[ApiProperty] attribute and writable:false was silently lost on child resources. Walk the parent class hierarchy to read the attribute from the declaring class; the child's own attribute still takes precedence. Fixes api-platform#8028
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
#[ApiProperty(writable: false)]declared on a private property inside a trait that is composed into an abstract parent class (e.g. a Doctrine STI parent) was silently ignored on child resources, leaving the property writable. PHP's reflection does not expose private parent/trait-declared properties on the child's ownReflectionClass, soAttributePropertyMetadataFactorynever read the attribute. Protected/public trait properties are unaffected (they remain visible on the child), which is why moving the trait into the subclass or usingprotected"worked".The factory now walks the parent class hierarchy and reads
#[ApiProperty]from the declaring class when the child's own reflection doesn't expose the property. The child's own attribute still takes precedence.Reproduction
Trait with a private
#[ApiProperty(writable: false)]property → abstract STI parent → concrete#[ApiResource]child with a public accessor: the property was accepted on write before the fix.Test plan
testPrivateTraitPropertyAttributeIsReadFromAbstractParent) + fixtures (trait, abstract parent, concrete child).src/Metadata/Tests/Property/green (38 tests). Two unrelated pre-existing failures in the wider suite confirmed on the clean base.Fixes #8028