Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa5d3d0

Browse files
committed
feature #38542 [FrameworkBundle][Serializer] Allow serializer default context configuration (soyuka)
This PR was merged into the 5.4 branch. Discussion ---------- [FrameworkBundle][Serializer] Allow serializer default context configuration | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | todo if we go with it <!-- required for new features --> This allow to configure the `context` for normalizer's on a high level: ```yaml serializer: default_context: enable_max_depth: true ``` This feature was asked in api-platform/core#1764 (comment) and I thought it could be in symfony instead. The messenger component has the same configuration. This re-opens symfony/symfony#28927 (for discussion). I'm still unsure about the discussion in symfony/symfony#28927 (comment) and what solution I should implement. Please let me know if you come to an agreement. Commits ------- 43d1ca5f86 Allow serializer default context configuration
2 parents 36b49ce + e9208f9 commit aa5d3d0

File tree

8 files changed

+21
-1
lines changed

8 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CHANGELOG
1616
* Add support for resetting container services after each messenger message
1717
* Add `configureContainer()`, `configureRoutes()`, `getConfigDir()` and `getBundlesPath()` to `MicroKernelTrait`
1818
* Add support for configuring log level, and status code by exception class
19+
* Bind the `default_context` parameter onto serializer's encoders and normalizers
1920

2021
5.3
2122
---

DependencyInjection/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,12 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode, callable $e
10051005
->end()
10061006
->end()
10071007
->end()
1008+
->arrayNode('default_context')
1009+
->normalizeKeys(false)
1010+
->useAttributeAsKey('name')
1011+
->defaultValue([])
1012+
->prototype('variable')->end()
1013+
->end()
10081014
->end()
10091015
->end()
10101016
->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,10 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
18131813
$defaultContext += ['max_depth_handler' => new Reference($config['max_depth_handler'])];
18141814
$container->getDefinition('serializer.normalizer.object')->replaceArgument(6, $defaultContext);
18151815
}
1816+
1817+
if (isset($config['default_context']) && $config['default_context']) {
1818+
$container->setParameter('serializer.default_context', $config['default_context']);
1819+
}
18161820
}
18171821

18181822
private function registerPropertyInfoConfiguration(ContainerBuilder $container, PhpFileLoader $loader)

Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@
266266
<xsd:complexType name="serializer">
267267
<xsd:choice minOccurs="0" maxOccurs="unbounded">
268268
<xsd:element name="mapping" type="file_mapping" />
269+
<xsd:element name="default-context" type="metadata" minOccurs="0" maxOccurs="1" />
269270
</xsd:choice>
270271
<xsd:attribute name="enabled" type="xsd:boolean" />
271272
<xsd:attribute name="enable-annotations" type="xsd:boolean" />

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ protected static function getBundleDefaultConfig()
446446
'enabled' => true,
447447
],
448448
'serializer' => [
449+
'default_context' => [],
449450
'enabled' => !class_exists(FullStack::class),
450451
'enable_annotations' => !class_exists(FullStack::class),
451452
'mapping' => ['paths' => []],

Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
'name_converter' => 'serializer.name_converter.camel_case_to_snake_case',
6868
'circular_reference_handler' => 'my.circular.reference.handler',
6969
'max_depth_handler' => 'my.max.depth.handler',
70+
'default_context' => ['enable_max_depth' => true],
7071
],
7172
'property_info' => true,
7273
'ide' => 'file%%link%%format',

Tests/DependencyInjection/Fixtures/xml/full.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
</framework:translator>
3434
<framework:validation enabled="true" />
3535
<framework:annotations cache="file" debug="true" file-cache-dir="%kernel.cache_dir%/annotations" />
36-
<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" />
36+
<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">
37+
<framework:default-context>
38+
<framework:enable_max_depth>true</framework:enable_max_depth>
39+
</framework:default-context>
40+
</framework:serializer>
3741
<framework:property-info />
3842
</framework:config>
3943
</container>

Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ framework:
5555
name_converter: serializer.name_converter.camel_case_to_snake_case
5656
circular_reference_handler: my.circular.reference.handler
5757
max_depth_handler: my.max.depth.handler
58+
default_context:
59+
enable_max_depth: true
5860
property_info: ~
5961
ide: file%%link%%format
6062
request:

0 commit comments

Comments
 (0)