Skip to content

Commit a14eb3f

Browse files
feature #44721 [Serializer] Deprecate support for abstract uid denormalization in UidNormalizer (fancyweb)
This PR was merged into the 6.1 branch. Discussion ---------- [Serializer] Deprecate support for abstract uid denormalization in UidNormalizer | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | - Continuation of #44600. All abstract classes should not be denormalized to Uuid::class. We need a concrete class. Instead of penalizing everyone with reflection in supportsDenormalization(), let's just bubble up the PHP \Error (we are going to do the same in UidValueResolver). Commits ------- 6fc4287 [Serializer] Deprecate support for abstract uid denormalization in UidNormalizer
2 parents 3eb26c1 + 6fc4287 commit a14eb3f

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

UPGRADE-6.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Serializer
2424
* Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead
2525
* Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead
2626
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead
27+
* Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
28+
* Deprecate denormalizing to an abstract class in `UidNormalizer`
2729

2830
Validator
2931
---------

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ CHANGELOG
1010
* Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead
1111
* Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead
1212
* Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead
13+
* Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
14+
* Deprecate denormalizing to an abstract class in `UidNormalizer`
1315

1416
6.0
1517
---

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@ public function supportsNormalization(mixed $data, string $format = null, array
7676
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
7777
{
7878
try {
79-
return AbstractUid::class !== $type ? $type::fromString($data) : Uuid::fromString($data);
79+
if (AbstractUid::class === $type) {
80+
trigger_deprecation('symfony/serializer', '6.1', 'Denormalizing to an abstract class in "%s" is deprecated.', __CLASS__);
81+
82+
return Uuid::fromString($data);
83+
}
84+
85+
return $type::fromString($data);
8086
} catch (\InvalidArgumentException|\TypeError $exception) {
8187
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The data is not a valid "%s" string representation.', $type), $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true);
82-
} catch (\Error $e) {
88+
} catch (\Error $e) { // @deprecated remove this catch block in 7.0
8389
if (str_starts_with($e->getMessage(), 'Cannot instantiate abstract class')) {
8490
return $this->denormalize($data, AbstractUid::class, $format, $context);
8591
}
@@ -93,7 +99,13 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
9399
*/
94100
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
95101
{
96-
return is_a($type, AbstractUid::class, true);
102+
if (AbstractUid::class === $type) {
103+
trigger_deprecation('symfony/serializer', '6.1', 'Supporting denormalization for the "%s" type in "%s" is deprecated, use one of "%s" child class instead.', AbstractUid::class, __CLASS__, AbstractUid::class);
104+
105+
return true;
106+
}
107+
108+
return is_subclass_of($type, AbstractUid::class, true);
97109
}
98110

99111
/**

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

Lines changed: 18 additions & 0 deletions
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.');
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.');
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy