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

Skip to content

Commit aac6436

Browse files
bug #46668 [FrameworkBundle] Lower JsonSerializableNormalizer priority (aprat84)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle] Lower JsonSerializableNormalizer priority | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | IMHO, more generic normalizers, like `ObjectNormalizer` or `JsonSerializableNormalizer` should have lower priority than other more specific normalizers, like `DateTimeNormalizer`, `ConstraintViolationListNormalizer`, etc. My issue was with `Carbon` library, which extends php's `DateTime`. `Carbon` classes implement `JsonSerializable` interface, so they are serialized with `JsonSerializableNormalizer`, using its internal `toJSON()` method, so I can't control which format I want them to be serialized. I should be able to do it, as I can with `DateTime`, cause both implement `DateTimeInterface`. That's because `JsonSerializableNormalizer` has higher priority than `DateTimeNormalizer`. For now, my "fix" was to change priority in config like so: ```yaml serializer.normalizer.json_serializable: class: Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer tags: - {name: 'serializer.normalizer', priority: -950 } ``` Commits ------- 3fcfc20 [FrameworkBundle] Lower JsonSerializableNormalizer priority
2 parents 07fa911 + 3fcfc20 commit aac6436

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
<service id="serializer.normalizer.json_serializable" class="Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer">
6262
<!-- Run before serializer.normalizer.object -->
63-
<tag name="serializer.normalizer" priority="-900" />
63+
<tag name="serializer.normalizer" priority="-950" />
6464
</service>
6565

6666
<service id="serializer.normalizer.problem" class="Symfony\Component\Serializer\Normalizer\ProblemNormalizer">

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ public function testJsonSerializableNormalizerRegistered()
12971297
$tag = $definition->getTag('serializer.normalizer');
12981298

12991299
$this->assertEquals(JsonSerializableNormalizer::class, $definition->getClass());
1300-
$this->assertEquals(-900, $tag[0]['priority']);
1300+
$this->assertEquals(-950, $tag[0]['priority']);
13011301
}
13021302

13031303
public function testObjectNormalizerRegistered()

0 commit comments

Comments
 (0)