[Serializer] Add #[ExtendsSerializationFor]
to declare new serialization attributes for a class
#61563
+225
−14
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.
This PR builds on #61532
It's a sibling of #61545
I 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-api
At 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.)
(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:
#[ExtendsSerializationFor(Target::class)]
are collected and validated: the container checks that members declared on the source exist on the target. If not, aMappingException
is thrown.