Skip to content

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Aug 26, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? yes
Issues -
License MIT

Very close to #61528
Prerequisite for #61287

At the moment, serialization attributes are read at runtime when framework.serialization.enable_attributes is 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_metadata resource 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:

        ->set('my_bundle.api_resource.product', Product::class)
            ->resourceTag('serializer.attribute_metadata')

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?)

@nicolas-grekas nicolas-grekas force-pushed the serializer.attribute_metadata branch from e21e3a8 to bc6e054 Compare September 3, 2025 17:28
@nicolas-grekas nicolas-grekas merged commit bde5221 into symfony:7.4 Sep 3, 2025
10 of 12 checks passed
@nicolas-grekas nicolas-grekas deleted the serializer.attribute_metadata branch September 3, 2025 17:51
fabpot added a commit that referenced this pull request Sep 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       | MIT

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.)

```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
This was referenced Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants