Skip to content

Commit 5d3f8f4

Browse files
Merge branch '7.3' into 7.4
* 7.3: [Serializer] Don't fallback to default serializer if tags specify a named one
2 parents ddd5cbf + 8ec0d38 commit 5d3f8f4

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-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-php85": "^1.32",
5960
"symfony/polyfill-uuid": "^1.15"
6061
},

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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,57 @@ public function testMultipleNamedSerializerTagsAreResolvedCorrectly()
377377
$this->assertCount(2, $encoderDefinition->getTag('serializer.encoder.api2'));
378378
}
379379

380+
#[DataProvider('provideEmptyTagsData')]
381+
public function testEmptyTagsAreIgnoredWhenNonEmptyArePresent(array $tagAttributesList, array $expectedDefaultTags, array $expectedApiTags)
382+
{
383+
$container = new ContainerBuilder();
384+
385+
$container->setParameter('kernel.debug', false);
386+
$container->setParameter('.serializer.named_serializers', ['api' => []]);
387+
388+
$container->register('serializer')->setArguments([null, null]);
389+
$container->register('n0')->addTag('serializer.normalizer', ['serializer' => ['default', 'api']]);
390+
$container->register('e0')->addTag('serializer.encoder', ['serializer' => ['default', 'api']]);
391+
392+
$normalizerDefinition = $container->register('n1')->setTags(['serializer.normalizer' => $tagAttributesList]);
393+
$encoderDefinition = $container->register('e1')->setTags(['serializer.encoder' => $tagAttributesList]);
394+
395+
$serializerPass = new SerializerPass();
396+
$serializerPass->process($container);
397+
398+
$this->assertSame($expectedDefaultTags, $normalizerDefinition->getTag('serializer.normalizer.default'));
399+
$this->assertSame($expectedApiTags, $normalizerDefinition->getTag('serializer.normalizer.api'));
400+
$this->assertSame($expectedDefaultTags, $encoderDefinition->getTag('serializer.encoder.default'));
401+
$this->assertSame($expectedApiTags, $encoderDefinition->getTag('serializer.encoder.api'));
402+
}
403+
404+
public static function provideEmptyTagsData(): iterable
405+
{
406+
yield 'with default name' => [
407+
[[], ['serializer' => 'default']],
408+
[[]],
409+
[],
410+
];
411+
412+
yield 'with non-default name' => [
413+
[[], ['serializer' => 'api']],
414+
[],
415+
[[]],
416+
];
417+
418+
yield 'with wildcard' => [
419+
[[], ['serializer' => '*']],
420+
[[]],
421+
[[]],
422+
];
423+
424+
yield 'with priority and no name' => [
425+
[[], ['priority' => 100]],
426+
[['priority' => 100]],
427+
[],
428+
];
429+
}
430+
380431
public function testThrowExceptionWhenNoNormalizersForNamedSerializers()
381432
{
382433
$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)