diff --git a/UPGRADE-6.1.md b/UPGRADE-6.1.md index 1c9d68e04655b..d0e7bbafb0a8c 100644 --- a/UPGRADE-6.1.md +++ b/UPGRADE-6.1.md @@ -24,6 +24,8 @@ Serializer * Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead * Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead * Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead + * Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead + * Deprecate denormalizing to an abstract class in `UidNormalizer` Validator --------- diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index 3f16b2ba42ac3..28b194edb8fca 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -10,6 +10,8 @@ CHANGELOG * Deprecate `ContextAwareDenormalizerInterface`, use `DenormalizerInterface` instead * Deprecate `ContextAwareEncoderInterface`, use `EncoderInterface` instead * Deprecate `ContextAwareDecoderInterface`, use `DecoderInterface` instead + * Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead + * Deprecate denormalizing to an abstract class in `UidNormalizer` 6.0 --- diff --git a/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php index 02d47e744b95c..f52fcc72f527b 100644 --- a/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php @@ -76,10 +76,16 @@ public function supportsNormalization(mixed $data, string $format = null, array public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed { try { - return AbstractUid::class !== $type ? $type::fromString($data) : Uuid::fromString($data); + if (AbstractUid::class === $type) { + trigger_deprecation('symfony/serializer', '6.1', 'Denormalizing to an abstract class in "%s" is deprecated.', __CLASS__); + + return Uuid::fromString($data); + } + + return $type::fromString($data); } catch (\InvalidArgumentException|\TypeError $exception) { throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The data is not a valid "%s" string representation.', $type), $data, [Type::BUILTIN_TYPE_STRING], $context['deserialization_path'] ?? null, true); - } catch (\Error $e) { + } catch (\Error $e) { // @deprecated remove this catch block in 7.0 if (str_starts_with($e->getMessage(), 'Cannot instantiate abstract class')) { return $this->denormalize($data, AbstractUid::class, $format, $context); } @@ -93,7 +99,13 @@ public function denormalize(mixed $data, string $type, string $format = null, ar */ public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool { - return is_a($type, AbstractUid::class, true); + if (AbstractUid::class === $type) { + 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); + + return true; + } + + return is_subclass_of($type, AbstractUid::class, true); } /** diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php index 14fa108668811..88fc7d4b5b766 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/UidNormalizerTest.php @@ -3,6 +3,7 @@ namespace Symfony\Component\Serializer\Tests\Normalizer; use PHPUnit\Framework\TestCase; +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; use Symfony\Component\Serializer\Exception\LogicException; use Symfony\Component\Serializer\Normalizer\UidNormalizer; use Symfony\Component\Uid\AbstractUid; @@ -16,6 +17,8 @@ class UidNormalizerTest extends TestCase { + use ExpectDeprecationTrait; + /** * @var UidNormalizer */ @@ -134,8 +137,13 @@ public function testSupportsDenormalizationForNonUid() $this->assertFalse($this->normalizer->supportsDenormalization('foo', \stdClass::class)); } + /** + * @group legacy + */ public function testSupportOurAbstractUid() { + $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.'); + $this->assertTrue($this->normalizer->supportsDenormalization('1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class)); } @@ -152,13 +160,23 @@ public function testDenormalize($uuidString, $class) $this->assertEquals($class::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class)); } + /** + * @group legacy + */ public function testDenormalizeOurAbstractUid() { + $this->expectDeprecation('Since symfony/serializer 6.1: Denormalizing to an abstract class in "Symfony\Component\Serializer\Normalizer\UidNormalizer" is deprecated.'); + $this->assertEquals(Uuid::fromString($uuidString = '1ea6ecef-eb9a-66fe-b62b-957b45f17e43'), $this->normalizer->denormalize($uuidString, AbstractUid::class)); } + /** + * @group legacy + */ public function testDenormalizeCustomAbstractUid() { + $this->expectDeprecation('Since symfony/serializer 6.1: Denormalizing to an abstract class in "Symfony\Component\Serializer\Normalizer\UidNormalizer" is deprecated.'); + $this->assertEquals(Uuid::fromString($uuidString = '1ea6ecef-eb9a-66fe-b62b-957b45f17e43'), $this->normalizer->denormalize($uuidString, TestAbstractCustomUid::class)); }