E5DF [Serializer] Remove normalizer cache in Serializer class by jvasseur · Pull Request #17140 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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: 0 additions & 2 deletions src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ private function normalizeObject($object, $format = null, array $context = array
foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof NormalizerInterface
&& $normalizer->supportsNormalization($object, $format)) {
$this->normalizerCache[$class][$format] = $normalizer;

Copy link
Contributor

Choose a reason for hiding this comment

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

those properties should be annotated with @deprecated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was planning to do a PR to master to deprecate them after this one is merged.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. 👍

return $normalizer->normalize($object, $format, $context);
}
Expand Down Expand Up @@ -272,7 +271,6 @@ private function denormalizeObject($data, $class, $format = null, array $context
foreach ($this->normalizers as $normalizer) {
if ($normalizer instanceof DenormalizerInterface
&& $normalizer->supportsDenormalization($data, $class, $format)) {
$this->denormalizerCache[$class][$format] = $normalizer;

return $normalizer->denormalize($data, $class, $format, $context);
}
Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,50 @@ public function testDenormalizeOnNormalizer()
$this->assertTrue($this->serializer->denormalize(json_encode($data), 'stdClass', 'json'));
}

public function testNormalizeWithSupportOnData()
{
$normalizer1 = $this->getMock('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
$normalizer1->method('supportsNormalization')
->willReturnCallback(function ($data, $format) {
return isset($data->test);
});
$normalizer1->method('normalize')->willReturn('test1');

$normalizer2 = $this->getMock('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
$normalizer2->method('supportsNormalization')
->willReturn(true);
$normalizer2->method('normalize')->willReturn('test2');

$serializer = new Serializer(array($normalizer1, $normalizer2));

$data = new \stdClass();
$data->test = true;
$this->assertEquals('test1', $serializer->normalize($data));

$this->assertEquals('test2', $serializer->normalize(new \stdClass()));
}

public function testDenormalizeWithSupportOnData()
{
$denormalizer1 = $this->getMock('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
$denormalizer1->method('supportsDenormalization')
->willReturnCallback(function ($data, $type, $format) {
return isset($data['test1']);
});
$denormalizer1->method('denormalize')->willReturn('test1');

$denormalizer2 = $this->getMock('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
$denormalizer2->method('supportsDenormalization')
->willReturn(true);
$denormalizer2->method('denormalize')->willReturn('test2');

$serializer = new Serializer(array($denormalizer1, $denormalizer2));

$this->assertEquals('test1', $serializer->denormalize(array('test1' => true), 'test'));

$this->assertEquals('test2', $serializer->denormalize(array(), 'test'));
}

public function testSerialize()
{
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
Expand Down
0