8000 [Serializer] Deprecate support for abstract uid denormalization in UidNormalizer by fancyweb · Pull Request #44721 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Deprecate support for abstract uid denormalization in UidNormalizer #44721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADE-6.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Comment on lines +27 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to me this signals the same deprecation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one deprecation for supportsDenormalization() that will return false instead of true for AbstractUid::class specifically.
And another for denormalize() that will throw an \Error if the class is abstract (instead of fallbacking to Uuid::class).


Validator
---------
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
18 changes: 15 additions & 3 deletions src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +17,8 @@

class UidNormalizerTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* @var UidNormalizer
*/
Expand Down Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand Down
0