-
-
Notifications
You must be signed in to change notification settings - Fork 9.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment has been minimized.
This comment has been minimized.
ecd355d
to
63409d5
Compare
This 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
to
f1c17d7
Compare
Good 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 |
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 up standard
and default
as name describing default stuff? Shouldn't we stick to default
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 | MIT Extracted 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 |
f1c17d7
to
764b46f
Compare
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/serializer.html.twig
Show resolved
Hide resolved
7d850a0
to
e3b4b23
Compare
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
to
d9ceb85
Compare
fabbot's suggestion looks legit, can you apply it? |
d9ceb85
to
0353498
Compare
@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 (like extended_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
to
ca834e5
Compare
ca834e5
to
29bd8ce
Compare
Thank you @HypeMC. |
Named serializers were introduced in symfony#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.`.
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 mentioned before.
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