[Serializer] Introduce named serializers #56823
Open
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.
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