Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[Serializer] Introduce named serializers#56823
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This comment has been minimized.
This comment has been minimized.
ecd355d
to63409d5
CompareThis PR was merged into the 7.1 branch.Discussion----------Fix singular phpdoc| Q | A| ------------- | ---| Branch? | 7.1| Bug fix? | no| New feature? | no <!-- please update src/**/CHANGELOG.md files -->| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->| Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->| License | MIT(Found while reviewing#56823)Commits-------e0f6258 Fix singular phpdoc
3e3ef44
tof1c17d7
CompareGood work! This will help improve performance and reduce the complexity of projects needing multiple serializers to deal with different contexts (ex: serializing data exposed through a public API and deserializing data coming from a third-party service). I'm +1 to merge this.
Maybe could we tweak the autoconfigurator to automatically exclude definitions already having an explicit |
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
That's a really great addition!
Just a quick question, why mixing upstandard
anddefault
as name describing default stuff? Shouldn't we stick todefault
only?
…hild()` shortcut method (HypeMC)This PR was merged into the 7.2 branch.Discussion----------[DependencyInjection] Add `ContainerBuilder::registerChild()` shortcut method| Q | A| ------------- | ---| Branch? | 7.2| Bug fix? | no| New feature? | yes| Deprecations? | no| Issues | -| License | MITExtracted from#56823 as suggested in#56823 (comment)Commits-------7ba430c [DependencyInjection] Add `ContainerBuilder::registerChild()` shortcut method
Reusing the same normalizer instances is a very bad idea as it would break for any normalizer implementing any of the |
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/serializer.html.twigShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Nice work
6b1ee4d
tod9ceb85
Comparefabbot's suggestion looks legit, can you apply it? |
@chalasr Done,https://github.com/symfony/symfony/compare/d9ceb8521073610eff9d338eea2a5558d3edbbc5..03534988f5e9cef66798ab9b36d38cdf3770e671 |
$names = array_unique(['default', ...$serializerNames]); | ||
} | ||
if ($tag['built-in'] ?? false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
To be consistent with the code base (likeextended_type
for Forms), I think it should be:
if ($tag['built-in'] ??false) { | |
if ($tag['built_in'] ??false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
aee8227
toca834e5
CompareThank you@HypeMC. |
ce228d3
intosymfony:7.2Uh oh!
There was an error while loading.Please reload this page.
Named serializers were introduced insymfony#56823 and they work great.We noticed a small bug when using custom name convertors.The MimeMessageNormalizer holds a reference to `serializer.normalizer.property`.But when using named serializers, it would use the specific child normalizer instead.With this change, we fix this problem for any service that starts with `serializer.normalizer.`.
Uh oh!
There was an error while loading.Please reload this page.
The idea behind this PR is to allow configuring multiple serializer instances with different default contexts, name converters, and sets of normalizers and encoders. This is useful when your application is communicating with multiple APIs, each with different rules. Similar ideas have been mentionedbefore.
The different serializers can be injected using named aliases:
Multiple normalizer/encoder instances with different arguments are created as child services of the default ones.
I also ensured that the same normalizer/encoder instances are reused between different serializers that have the same default context and name converter to minimize the number of child services created.Custom normalizers/encoders can target specific serializers using the
serializer
tag attribute:For BC reasons, not setting the
serializer
tag attribute is the same as setting it to the default one:The profiler has been updated to support multiple serializer instances:
All normalizers/encoders are tagged with additional named serializer specific tags to help with debugging. To get the priority of normalizers/encoders for a certain serializer, use the
serializer.normalizer.<name>
orserializer.encoder.<name>
tags:Since Symfony comes with some pre-registered normalizers and encoders, I added options to exclude those in case someone wants to use only custom ones:
TBH, I have doubts about the usefulness of this, please let me know your thoughts.
I've split the PR into two commits to ease reviewing:
SerializerPass
without adding any features