Skip to content

[Serializer] Use DenormalizerAwareInterface instead of SerializerAwareInterface #52764

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

Open
wants to merge 4 commits into
base: 7.4
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

7.1
---

* Let `UnwrappingDenormalizer` implement `DenormalizerAwareInterface` instead of `SerializerAwareInterface`
* Deprecate method `UnwrappingDenormalizer::setSerializer()`, use `UnwrappingDenormalizer::setDenormalizer()` instead

7.0
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @author Eduard Bulava <bulavaeduard@gmail.com>
*/
final class UnwrappingDenormalizer implements DenormalizerInterface, SerializerAwareInterface
Copy link
Member

Choose a reason for hiding this comment

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

I think we should keep SerializerAwareInterface as long as we implement setSerializer().

Copy link
Member

Choose a reason for hiding this comment

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

If we implement SerializerAwareInterface, the Serializer class will be calling the deprecated method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's the reason why I removed SerializerAwareInterface. The Serializer class will call setSerializer() here: https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/Serializer/Serializer.php#L85

But without it, its a bc-break for code relying on UnwrappingDenormalizer implementing SerializerAwareInterface. I am not sure how to solve this problem.

final class UnwrappingDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface
{
use SerializerAwareTrait;
use DenormalizerAwareTrait;

public const UNWRAP_PATH = 'unwrap_path';

Expand Down Expand Up @@ -51,15 +50,25 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
$data = $this->propertyAccessor->getValue($data, $propertyPath);
}

if (!$this->serializer instanceof DenormalizerInterface) {
throw new LogicException('Cannot unwrap path because the injected serializer is not a denormalizer.');
}

return $this->serializer->denormalize($data, $type, $format, $context);
return $this->denormalizer->denormalize($data, $type, $format, $context);
}

public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
{
return \array_key_exists(self::UNWRAP_PATH, $context) && !isset($context['unwrapped']);
}

/**
* @deprecated Since symfony/serializer 7.1: The "setSerializer()" method is deprecated, use "setDenormalizer()" instead.
*/
public function setSerializer(SerializerInterface $serializer): void
{
trigger_deprecation('symfony/serializer', '7.1', 'The "%s()" method is deprecated, use "setDenormalizer()" instead.', __METHOD__);

if (!$serializer instanceof DenormalizerInterface) {
throw new LogicException(sprintf('Cannot set denormalizer because the injected serializer does not implement the "%s".', DenormalizerInterface::class));
}

$this->setDenormalizer($serializer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummy;

/**
Expand All @@ -23,13 +23,13 @@
class UnwrappinDenormalizerTest extends TestCase
{
private UnwrappingDenormalizer $denormalizer;
private MockObject&Serializer $serializer;
private MockObject&DenormalizerInterface $baseDenormalizer;

protected function setUp(): void
{
$this->serializer = $this->createMock(Serializer::class);
$this->baseDenormalizer = $this->createMock(DenormalizerInterface::class);
$this->denormalizer = new UnwrappingDenormalizer();
$this->denormalizer->setSerializer($this->serializer);
$this->denormalizer->setDenormalizer($this->baseDenormalizer);
}

public function testSupportsNormalization()
Expand All @@ -46,7 +46,7 @@ public function testDenormalize()
$expected->bar = 'bar';
$expected->setFoo('foo');

$this->serializer->expects($this->exactly(1))
$this->baseDenormalizer->expects($this->exactly(1))
->method('denormalize')
->with(['foo' => 'foo', 'bar' => 'bar', 'baz' => true])
->willReturn($expected);
Expand All @@ -65,7 +65,7 @@ public function testDenormalize()

public function testDenormalizeInvalidPath()
{
$this->serializer->expects($this->exactly(1))
$this->baseDenormalizer->expects($this->exactly(1))
->method('denormalize')
->with(null)
->willReturn(new ObjectDummy());
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Serializer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"require": {
"php": ">=8.2",
"symfony/polyfill-ctype": "~1.8"
"symfony/polyfill-ctype": "~1.8",
"symfony/deprecation-contracts": "^2.5|^3"
},
"require-dev": {
"phpdocumentor/reflection-docblock": "^3.2|^4.0|^5.0",
Expand Down