Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Serializer] Allow using attributes to declare compile-time serialization metadata#61532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
nicolas-grekas merged 1 commit intosymfony:7.4fromnicolas-grekas:serializer.attribute_metadataSep 3, 2025
Merged
[Serializer] Allow using attributes to declare compile-time serialization metadata#61532
nicolas-grekas merged 1 commit intosymfony:7.4fromnicolas-grekas:serializer.attribute_metadataSep 3, 2025
Uh oh!
There was an error while loading.Please reload this page.
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
5e4db22 to8c3e9c1Compare24ccf61 toe21e3a8Comparee21e3a8 tobc6e054Comparebde5221 intosymfony:7.4 10 of 12 checks passed
Uh oh!
There was an error while loading.Please reload this page.
fabpot added a commit that referenced this pull requestSep 10, 2025
…re new serialization attributes for a class (nicolas-grekas)This PR was merged into the 7.4 branch.Discussion----------[Serializer] Add `#[ExtendsSerializationFor]` to declare new serialization attributes for a class| Q | A| ------------- | ---| Branch? | 7.4| Bug fix? | no| New feature? | yes| Deprecations? | no| Issues | -| License | MITThis PR builds on#61532It's a sibling of#61545I propose to add a `#[ExtendsSerializationFor]` attribute that allows adding serialization attributes to another class.This is typically needed for third party classes. For context, Sylius has a nice doc about this:https://docs.sylius.com/the-customization-guide/customizing-serialization-of-apiAt the moment, the only way to achieve this is by declaring the new attributes in the (hardcoded) `config/serialization/` folder, using either xml or yaml. No attributes.With this PR, one will be able to define those extra serialization attributes using PHP attributes, set on classes that'd mirror the properties/getters of the targeted class. The compiler pass will ensure that all properties/getters declared in these source classes also exist in the target class. (source = the app's class that declares the new serialization attributes; target = the existing class to add serialization attributes to.)```php#[ExtendsSerializationFor(TargetClass::class)]abstract class SourceClass{ #[Groups(['my_app'])] #[SerializedName('fullName')] public string $name = ''; #[Groups(['my_app'])] public string $email = ''; #[Groups(['my_app'])] #[MaxDepth(2)] public Category $category;}```(I made the class abstract because it's not supposed to be instantiated - but it's not mandatory.)Here are the basics of how this works:1. During container compilation, classes marked with `#[ExtendsSerializationFor(Target::class)]` are collected and validated: the container checks that members declared on the source exist on the target. If not, a `MappingException` is thrown.2. The serializer is configured to map the target to its source classes.3. At runtime, when loading serialization metadata for the target, attributes (groups, serialized names, max depth, etc.) are read from both the target and its mapped source classes and applied accordingly.Commits-------386f949 [Serializer] Add `#[ExtendsSerializationFor]` to declare new serialization attributes for a class
Sign up for freeto 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.
Uh oh!
There was an error while loading.Please reload this page.
Very close to#61528
Prerequisite for#61287
At the moment, serialization attributes are read at runtime when
framework.serialization.enable_attributesis true.This means they don't fit for bundles nor can't they be warmed up.
This PR fixes both issues by using a new
serializer.attribute_metadataresource tag, that's turned into a list of classes to parse for attributes at compile-time.For bundles and for apps, the tag is added by explicit service configuration:
Unlike validation where we have constraint attributes to auto-discover service resources, serialization doesn't have any corresponding hooks. We do have a few like
#[DiscriminatorMap]of#[Groups], but relying on those would miss many more classes that are meant for serialization. Maybe we could introduce an attribute that'd hint that some class is serializable by the component (and require the attribute at some point in the future?)