Skip to content

fix(metadata): read ApiProperty from trait private properties inherited via parent class#8275

Merged
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/sti-trait-apiproperty-8028
Jun 10, 2026
Merged

fix(metadata): read ApiProperty from trait private properties inherited via parent class#8275
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/sti-trait-apiproperty-8028

Conversation

@soyuka

@soyuka soyuka commented Jun 10, 2026

Copy link
Copy Markdown
Member

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 own ReflectionClass, so AttributePropertyMetadataFactory never 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 using protected "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

  • Added failing test (testPrivateTraitPropertyAttributeIsReadFromAbstractParent) + fixtures (trait, abstract parent, concrete child).
  • Test passes after the fix.
  • src/Metadata/Tests/Property/ green (38 tests). Two unrelated pre-existing failures in the wider suite confirmed on the clean base.

Fixes #8028

…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
@soyuka soyuka merged commit 6b8bd0a into api-platform:4.3 Jun 10, 2026
112 checks passed
@soyuka soyuka deleted the fix/sti-trait-apiproperty-8028 branch June 10, 2026 12:41
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.

1 participant