8000 feature #38542 [FrameworkBundle][Serializer] Allow serializer default… · symfony/symfony@cddd1ff · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit cddd1ff

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 #28927 (for discussion). I'm still unsure about the discussion in #28927 (comment) and what solution I should implement. Please let me know if you come to an agreement. Commits ------- 43d1ca5 Allow serializer default context configuration
2 parents a76d746 + 43d1ca5 commit cddd1ff

File tree

10 files changed

+49
-1
lines changed

10 files changed

+49
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/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
---

src/Symfony/Bundle/FrameworkBundle/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()

src/Symfony/Bundle/FrameworkBundle/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)

src/Symfony/Bundle/FrameworkBundle/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" />

src/Symfony/Bundle/FrameworkBundle/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' => []],

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 1 addition & 0 deletions
< A3DB tr class="diff-line-row">
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',

src/Symfony/Bundle/FrameworkBundle/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>

src/Symfony/Bundle/FrameworkBundle/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:

src/Symfony/Component/Serializer/DependencyInjection/SerializerPass.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Serializer\DependencyInjection;
1313

14+
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
1415
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1516
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -60,5 +61,17 @@ public function process(ContainerBuilder $container)
6061
}
6162

6263
$serializerDefinition->replaceArgument(1, $encoders);
64+
65+
if (!$container->hasParameter('serializer.default_context')) {
66+
return;
67+
}
68+
69+
$defaultContext = $container->getParameter('serializer.default_context');
70+
foreach (array_keys(array_merge($container->findTaggedServiceIds($this->normalizerTag), $container->findTaggedServiceIds($this->encoderTag))) as $service) {
71+
$definition = $container->getDefinition($service);
72+
$definition->setBindings(['array $defaultContext' => new BoundArgument($defaultContext, false)] + $definition->getBindings());
73+
}
74+
75+
$container->getParameterBag()->remove('serializer.default_context');
6376
}
6477
}

src/Symfony/Component/Serializer/Tests/DependencyInjection/SerializerPassTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
@@ -68,4 +69,18 @@ public function testServicesAreOrderedAccordingToPriority()
6869
$this->assertEquals($expected, $definition->getArgument(0));
6970
$this->assertEquals($expected, $definition->getArgument(1));
7071
}
72+
73+
public function testBindSerializerDefaultContext()
74+
{
75+
$container = new ContainerBuilder();
76+
$container->register('serializer')->setArguments([null, null]);
77+
$container->setParameter('serializer.default_context', ['enable_max_depth' => true]);
78+
$definition = $container->register('n1')->addTag('serializer.normalizer')->addTag('serializer.encoder');
79+
80+
$serializerPass = new SerializerPass();
81+
$serializerPass->process($container);
82+
83+
$bindings = $definition->getBindings();
84+
$this->assertEquals($bindings['array $defaultContext'], new BoundArgument(['enable_max_depth' => true], false));
85+
}
7186
}

0 commit comments

Comments
 (0)
0