-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Allow serializer context configuration #28927
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,11 @@ | |
</framework:translator> | ||
<framework:validation enabled="true" /> | ||
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" /> | ||
<framework:serializer enabled="true" enable-annotations="true" name-converter="serializer.name_converter.camel_case_to_snake_case" circular-reference-handler="my.circular.reference.handler" max-depth-handler="my.max.depth.handler" /> | ||
<framework:serializer enabled="true" enable-annotations="true" name-converter="serializer.name_converter.camel_case_to_snake_case" circular-reference-handler="my.circular.reference.handler" max-depth-handler="my.max.depth.handler"> | ||
<framework:default-context> | ||
<framework:enable_max_depth>true</framework:enable_max_depth> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this key allow any "array-like" structure? Shouldn't the XSD be updated accordingly? |
||
</framework:default-context> | ||
</framework:serializer> | ||
<framework:property-info /> | ||
</framework:config> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,11 +50,39 @@ public function process(ContainerBuilder $container) | |
|
||
$serializerDefinition = $container->getDefinition($this->serializerService); | ||
$serializerDefinition->replaceArgument(0, $normalizers); | ||
$this->addDefaultContextParameter($normalizers, $container); | ||
|
||
if (!$encoders = $this->findAndSortTaggedServices($this->encoderTag, $container)) { | ||
throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->encoderTag, $this->serializerService)); | ||
} | ||
|
||
$serializerDefinition->replaceArgument(1, $encoders); | ||
$this->addDefaultContextParameter($encoders, $container); | ||
} | ||
|
||
private function addDefaultContextParameter($services, $container) | ||
{ | ||
foreach ($services as $service) { | ||
$definition = $container->getDefinition($service); | ||
if (!$definition->isAutowired()) { | ||
continue; | ||
} | ||
|
||
if (null === $class = $definition->getClass()) { | ||
continue; | ||
} | ||
|
||
$reflection = new \ReflectionClass($class); | ||
|
||
if (null === $constructor = $reflection->getConstructor()) { | ||
continue; | ||
} | ||
|
||
foreach ($constructor->getParameters() as $arg) { | ||
if ('defaultContext' === $arg->name) { | ||
$definition->setArgument('$defaultContext', '%serializer.default_context%'); | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't love this level of magic. I'd prefer: A) Just documenting the parameter and allowing people to wire it up manually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer B) because it will work with projects providing extra context options such as API Platform, and doing it manually is tedious. What we could do:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other better option: create a context builder that returns an array. Then it will be consistent with the builder introduced in HttpClient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussion about this in #30888 |
||
} | ||
} |
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.
This doesn't allow for things like the
circular_reference_handler
. I mean, you can set it, but there's no way to set this to a service, for example - none of the handler stuff will work. We need to check for the handler options explicitly inFrameworkExtension
and convert them to References. In the docs, I guess we'll need to document these handlers as services that should use the__invoke()
method.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.
Maybe could be also do something more generic: check if a service corresponding to a given string exists, if it's the case, convert it to a reference. WDYT?