Skip to content

Commit 09000e1

Browse files
committed
[Serializer] Deprecate support for abstract uid denormalization in UidNormalizer
1 parent 0f03f94 commit 09000e1

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

UPGRADE-6.1.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
UPGRADE FROM 6.0 to 6.1
2+
=======================
3+
4+
Serializer
5+
----------
6+
7+
* Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
8+
* Deprecate denormalizing to an abstract class in `UidNormalizer`

UPGRADE-7.0.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
UPGRADE FROM 6.x to 7.0
2+
=======================
3+
4+
Serializer
5+
----------
6+
7+
* Remove denormalization support for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
8+
* Denormalizing to an abstract class in `UidNormalizer` now throws an `\Error`

src/Symfony/Component/Serializer/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
8+
* Deprecate denormalizing to an abstract class in `UidNormalizer`
9+
410
6.0
511
---
612

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ public function supportsNormalization(mixed $data, string $format = null): bool
7070
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
7171
{
7272
try {
73-
return AbstractUid::class !== $type ? $type::fromString($data) : Uuid::fromString($data);
73+
if (AbstractUid::class === $type) {
74+
trigger_deprecation('symfony/serializer', '6.1', sprintf('Denormalizing to an abstract class in "%s" is deprecated, it will throw an \Error in 7.0.', __CLASS__));
75+
76+
return Uuid::fromString($data);
77+
}
78+
79+
return $type::fromString($data);
7480
} catch (\InvalidArgumentException $exception) {
7581
throw NotNormalizableValueException::createForUnexpectedDataType('The data is not a valid UUID string representation.', $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
7682
} catch (\TypeError $exception) {
@@ -89,6 +95,10 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
8995
*/
9096
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
9197
{
98+
if (AbstractUid::class === $type) {
99+
trigger_deprecation('symfony/serializer', '6.1', sprintf('Supporting denormalization for the "%s" type in "%s" is deprecated, use one of "%s" child class instead.', AbstractUid::class, __CLASS__, AbstractUid::class));
100+
}
101+
92102
return is_a($type, AbstractUid::class, true);
93103
}
94104

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

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Component\Serializer\Tests\Normalizer;
44

55
use PHPUnit\Framework\TestCase;
6+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
67
use Symfony\Component\Serializer\Exception\LogicException;
78
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
89
use Symfony\Component\Uid\AbstractUid;
@@ -16,6 +17,8 @@
1617

1718
class UidNormalizerTest extends TestCase
1819
{
20+
use ExpectDeprecationTrait;
21+
1922
/**
2023
* @var UidNormalizer
2124
*/
@@ -134,8 +137,13 @@ public function testSupportsDenormalizationForNonUid()
134137
$this->assertFalse($this->normalizer->supportsDenormalization('foo', \stdClass::class));
135138
}
136139

140+
/**
141+
* @group legacy
142+
*/
137143
public function testSupportOurAbstractUid()
138144
{
145+
$this->expectDeprecation('Since symfony/serializer 6.1: Supporting denormalization for the "Symfony\Component\Uid\AbstractUid" type in "Symfony\Component\Serializer\Normalizer\UidNormalizer" is deprecated, use one of "Symfony\Component\Uid\AbstractUid" child class instead.');
146+
139147
$this->assertTrue($this->normalizer->supportsDenormalization('1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class));
140148
}
141149

@@ -152,13 +160,23 @@ public function testDenormalize($uuidString, $class)
152160
$this->assertEquals($class::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class));
153161
}
154162

163+
/**
164+
* @group legacy
165+
*/
155166
public function testDenormalizeOurAbstractUid()
156167
{
168+
$this->expectDeprecation('Since symfony/serializer 6.1: Denormalizing to an abstract class in "Symfony\Component\Serializer\Normalizer\UidNormalizer" is deprecated, it will throw an \Error in 7.0.');
169+
157170
$this->assertEquals(Uuid::fromString($uuidString = '1ea6ecef-eb9a-66fe-b62b-957b45f17e43'), $this->normalizer->denormalize($uuidString, AbstractUid::class));
158171
}
159172

173+
/**
174+
* @group legacy
175+
*/
160176
public function testDenormalizeCustomAbstractUid()
161177
{
178+
$this->expectDeprecation('Since symfony/serializer 6.1: Denormalizing to an abstract class in "Symfony\Component\Serializer\Normalizer\UidNormalizer" is deprecated, it will throw an \Error in 7.0.');
179+
162180
$this->assertEquals(Uuid::fromString($uuidString = '1ea6ecef-eb9a-66fe-b62b-957b45f17e43'), $this->normalizer->denormalize($uuidString, TestAbstractCustomUid::class));
163181
}
164182

0 commit comments

Comments
 (0)