Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Serializer] Add#[ExtendsSerializationFor] to declare new serialization attributes for a class#61563

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
fabpot merged 1 commit intosymfony:7.4fromnicolas-grekas:serializer.for
Sep 10, 2025

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedAug 30, 2025
edited
Loading

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
Issues-
LicenseMIT

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

#[ExtendsSerializationFor(TargetClass::class)]abstractclass SourceClass{    #[Groups(['my_app'])]    #[SerializedName('fullName')]publicstring$name ='';    #[Groups(['my_app'])]publicstring$email ='';    #[Groups(['my_app'])]    #[MaxDepth(2)]publicCategory$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, aMappingException 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.

@carsonbotcarsonbot added this to the7.4 milestoneAug 30, 2025
@OskarStarkOskarStark changed the title[Serializer] Add #[ExtendsSerializationFor] to declare new serialization attributes for a class[Serializer] Add#[ExtendsSerializationFor] to declare new serialization attributes for a classSep 1, 2025
fabpot added a commit that referenced this pull requestSep 2, 2025
…ct classes for resource definitions (nicolas-grekas)This PR was merged into the 7.4 branch.Discussion----------[DependencyInjection] Parse attributes found on abstract classes for resource definitions| Q             | A| ------------- | ---| Branch?       | 7.4| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Issues        | -| License       | MITThis PR improves support for resource definitions by allowing to parse attributes on abstract classes.This can be useful for PRs like#61563 and#61545, so that the attribute proposed there could be set on abstract classes.Commits-------6571f7b [DependencyInjection] Parse attributes found on abstract classes for resource definitions
@fabpot
Copy link
Member

Thank you@nicolas-grekas.

@fabpotfabpot merged commite0b50a7 intosymfony:7.4Sep 10, 2025
10 of 12 checks passed
@nicolas-grekasnicolas-grekas deleted the serializer.for branchSeptember 10, 2025 12:12
This was referencedOct 27, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@alexandre-dauboisalexandre-dauboisalexandre-daubois approved these changes

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

7.4

Development

Successfully merging this pull request may close these issues.

4 participants

@nicolas-grekas@fabpot@alexandre-daubois@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp