Skip to content

Commit 8ec0d38

Browse files
committed
bug symfony#61526 [Serializer] Don't fallback to default serializer if tags specify a named one (HypeMC)
This PR was merged into the 7.3 branch. Discussion ---------- [Serializer] Don't fallback to default serializer if tags specify a named one | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT An alternative to symfony#61511. Commits ------- e442a38 [Serializer] Don't fallback to default serializer if tags specify a named one
2 parents c87fabb + e442a38 commit 8ec0d38

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"symfony/polyfill-intl-normalizer": "~1.0",
5656
"symfony/polyfill-mbstring": "~1.0",
5757
"symfony/polyfill-php83": "^1.28",
58+
"symfony/polyfill-php84": "^1.30",
5859
"symfony/polyfill-uuid": "^1.15"
5960
},
6061
"replace": {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ private function createNamedSerializerTags(ContainerBuilder $container, string $
8989
foreach ($container->findTaggedServiceIds($tagName) as $serviceId => $tags) {
9090
$definition = $container->getDefinition($serviceId);
9191

92+
if (array_any($tags, $closure = fn (array $tag) => (bool) $tag)) {
93+
$tags = array_filter($tags, $closure);
94+
}
95+
9296
foreach ($tags as $tag) {
9397
$names = (array) ($tag['serializer'] ?? []);
9498

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,62 @@ public function testMultipleNamedSerializerTagsAreResolvedCorrectly()
381381
$this->assertCount(2, $encoderDefinition->getTag('serializer.encoder.api2'));
382382
}
383383

384+
/**
385+
* @dataProvider provideEmptyTagsData
386+
*/
387+
public function testEmptyTagsAreIgnoredWhenNonEmptyArePresent(
388+
array $tagAttributesList,
389+
array $expectedDefaultTags,
390+
array $expectedApiTags,
391+
) {
392+
$container = new ContainerBuilder();
393+
394+
$container->setParameter('kernel.debug', false);
395+
$container->setParameter('.serializer.named_serializers', ['api' => []]);
396+
397+
$container->register('serializer')->setArguments([null, null]);
398+
$container->register('n0')->addTag('serializer.normalizer', ['serializer' => ['default', 'api']]);
399+
$container->register('e0')->addTag('serializer.encoder', ['serializer' => ['default', 'api']]);
400+
401+
$normalizerDefinition = $container->register('n1')->setTags(['serializer.normalizer' => $tagAttributesList]);
402+
$encoderDefinition = $container->register('e1')->setTags(['serializer.encoder' => $tagAttributesList]);
403+
404+
$serializerPass = new SerializerPass();
405+
$serializerPass->process($container);
406+
407+
$this->assertSame($expectedDefaultTags, $normalizerDefinition->getTag('serializer.normalizer.default'));
408+
$this->assertSame($expectedApiTags, $normalizerDefinition->getTag('serializer.normalizer.api'));
409+
$this->assertSame($expectedDefaultTags, $encoderDefinition->getTag('serializer.encoder.default'));
410+
$this->assertSame($expectedApiTags, $encoderDefinition->getTag('serializer.encoder.api'));
411+
}
412+
413+
public static function provideEmptyTagsData(): iterable
414+
{
415+
yield 'with default name' => [
416+
[[], ['serializer' => 'default']],
417+
[[]],
418+
[],
419+
];
420+
421+
yield 'with non-default name' => [
422+
[[], ['serializer' => 'api']],
423+
[],
424+
[[]],
425+
];
426+
427+
yield 'with wildcard' => [
428+
[[], ['serializer' => '*']],
429+
[[]],
430+
[[]],
431+
];
432+
433+
yield 'with priority and no name' => [
434+
[[], ['priority' => 100]],
435+
[['priority' => 100]],
436+
[],
437+
];
438+
}
439+
384440
public function testThrowExceptionWhenNoNormalizersForNamedSerializers()
385441
{
386442
$container = new ContainerBuilder();

src/Symfony/Component/Serializer/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": ">=8.2",
2020
"symfony/deprecation-contracts": "^2.5|^3",
21-
"symfony/polyfill-ctype": "~1.8"
21+
"symfony/polyfill-ctype": "~1.8",
22+
"symfony/polyfill-php84": "^1.30"
2223
},
2324
"require-dev": {
2425
"phpdocumentor/reflection-docblock": "^3.2|^4.0|^5.0",

0 commit comments

Comments
 (0)