Skip to content

fix(metadata): preserve explicit class on ApiResource when propagating defaults#8262

Open
soyuka wants to merge 1 commit into
api-platform:4.3from
soyuka:fix/apiresource-class-propagation-7187
Open

fix(metadata): preserve explicit class on ApiResource when propagating defaults#8262
soyuka wants to merge 1 commit into
api-platform:4.3from
soyuka:fix/apiresource-class-propagation-7187

Conversation

@soyuka

@soyuka soyuka commented Jun 8, 2026

Copy link
Copy Markdown
Member

Summary

When #[ApiResource(class: SomeClass::class)] is set explicitly on a resource, the factory was overwriting the user-provided class with the PHP class name during default propagation. As a result, the explicit class never reached cascaded operations.

The change in OperationDefaultsTrait::getResourceWithDefaults() now falls back to the PHP class name only when no class is already set on the resource.

Maintainer directive (from issue #7187): "you shouldn't really change the class but indeed we should fix this".

Reproduction

#[ApiResource(class: ResourceClassPropagationTarget::class)]
final class ResourceClassPropagation {}

Before the fix, every operation's getClass() returned ResourceClassPropagation::class, ignoring the explicit attribute argument. After the fix, both the resource and every cascaded operation expose ResourceClassPropagationTarget::class.

Test plan

  • Added failing test testCreatePropagatesExplicitResourceClass.
  • Test passes after the fix.
  • Full AttributesResourceMetadataCollectionFactoryTest class still green (12 tests, 31 assertions).
  • tests/Functional/OverriddenOperationTest.php still green (10 tests, 20 assertions).
  • php-cs-fixer + phpstan clean on changed files.

Fixes #7187

…g defaults

When `#[ApiResource(class: SomeClass::class)]` is set explicitly, the
factory overwrote it with the PHP class name during default propagation,
preventing the explicit class from reaching cascaded operations. Fall back
to the PHP class name only when no class is set.

Fixes api-platform#7187
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