Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fdc61b4

Browse filesBrowse files
bug #44600 [Serializer] Fix denormalizing custom class in UidNormalizer (fancyweb)
This PR was merged into the 5.3 branch. Discussion ---------- [Serializer] Fix denormalizing custom class in UidNormalizer | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Considering we support denormalizing any subclass of `AbstractUid::class` we should not denormalize any `$type !== Ulid::class` to `Uuid::class` because this is wrong. We need to call `$type::fromString` (see `TestCustomUid`). But `fromString()` is an abstract method, so it cannot be called directly on abstract classes, I guess we should have not supported them from the start. However, since they work right now (since all abstract classes fall back to the `Uuid:class` behavior) we cannot break it. So I propose to deprecate supporting abstract classes in 6.1 and remove it in 7.0. Commits ------- ba9e002 [Serializer] Fix denormalizing custom class in UidNormalizer
2 parents f5235a9 + ba9e002 commit fdc61b4
Copy full SHA for fdc61b4

File tree

2 files changed

+37
-8
lines changed
Filter options

2 files changed

+37
-8
lines changed

‎src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php
+7-2Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Serializer\Exception\LogicException;
1515
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1616
use Symfony\Component\Uid\AbstractUid;
17-
use Symfony\Component\Uid\Ulid;
1817
use Symfony\Component\Uid\Uuid;
1918

2019
final class UidNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
@@ -70,9 +69,15 @@ public function supportsNormalization($data, string $format = null)
7069
public function denormalize($data, string $type, string $format = null, array $context = [])
7170
{
7271
try {
73-
return Ulid::class === $type ? Ulid::fromString($data) : Uuid::fromString($data);
72+
return AbstractUid::class !== $type ? $type::fromString($data) : Uuid::fromString($data);
7473
} catch (\InvalidArgumentException $exception) {
7574
throw new NotNormalizableValueException(sprintf('The data is not a valid "%s" string representation.', $type));
75+
} catch (\Error $e) {
76+
if (str_starts_with($e->getMessage(), 'Cannot instantiate abstract class')) {
77+
return $this->denormalize($data, AbstractUid::class, $format, $context);
78+
}
79+
80+
throw $e;
7681
}
7782
}
7883

‎src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php
+30-6Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public function dataProvider()
116116
['4126dbc1-488e-4f6e-aadd-775dcbac482e', UuidV4::class],
117117
['18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22', UuidV5::class],
118118
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', UuidV6::class],
119-
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class],
120119
['01E4BYF64YZ97MDV6RH0HAMN6X', Ulid::class],
120+
['01FPT3YXZXJ1J437FES7CR5BCB', TestCustomUid::class],
121121
];
122122
}
123123

@@ -134,16 +134,32 @@ public function testSupportsDenormalizationForNonUid()
134134
$this->assertFalse($this->normalizer->supportsDenormalization('foo', \stdClass::class));
135135
}
136136

137+
public function testSupportOurAbstractUid()
138+
{
139+
$this->assertTrue($this->normalizer->supportsDenormalization('1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class));
140+
}
141+
142+
public function testSupportCustomAbstractUid()
143+
{
144+
$this->assertTrue($this->normalizer->supportsDenormalization('ccc', TestAbstractCustomUid::class));
145+
}
146+
137147
/**
138148
* @dataProvider dataProvider
139149
*/
140150
public function testDenormalize($uuidString, $class)
141151
{
142-
if (Ulid::class === $class) {
143-
$this->assertEquals(new Ulid($uuidString), $this->normalizer->denormalize($uuidString, $class));
144-
} else {
145-
$this->assertEquals(Uuid::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class));
146-
}
152+
$this->assertEquals($class::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class));
153+
}
154+
155+
public function testDenormalizeOurAbstractUid()
156+
{
157+
$this->assertEquals(Uuid::fromString($uuidString = '1ea6ecef-eb9a-66fe-b62b-957b45f17e43'), $this->normalizer->denormalize($uuidString, AbstractUid::class));
158+
}
159+
160+
public function testDenormalizeCustomAbstractUid()
161+
{
162+
$this->assertEquals(Uuid::fromString($uuidString = '1ea6ecef-eb9a-66fe-b62b-957b45f17e43'), $this->normalizer->denormalize($uuidString, TestAbstractCustomUid::class));
147163
}
148164

149165
public function testNormalizeWithNormalizationFormatPassedInConstructor()
@@ -169,3 +185,11 @@ public function testNormalizeWithNormalizationFormatNotValid()
169185
]);
170186
}
171187
}
188+
189+
class TestCustomUid extends Ulid
190+
{
191+
}
192+
193+
abstract class TestAbstractCustomUid extends Ulid
194+
{
195+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.