Skip to content

Commit 61505e6

Browse files
committed
bug #17140 [Serializer] Remove normalizer cache in Serializer class (jvasseur)
This PR was merged into the 2.3 branch. Discussion ---------- [Serializer] Remove normalizer cache in Serializer class | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | The serializer cache the normalizer/denormalizer to use based only on the class and format. But the supportsNormalization and supportDenormalization methods can decide based on the data passed leading to hard to find bugs when the serializer used a cached normalizer it shouldn't use. Commits ------- 8566dc1 Remove normalizer cache in Serializer class
2 parents e87d822 + 166531b commit 61505e6

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Serializer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ private function normalizeObject($object, $format = null, array $context = array
237237
foreach ($this->normalizers as $normalizer) {
238238
if ($normalizer instanceof NormalizerInterface
239239
&& $normalizer->supportsNormalization($object, $format)) {
240-
$this->normalizerCache[$class][$format] = $normalizer;
241240

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

277275
return $normalizer->denormalize($data, $class, $format, $context);
278276
}

Tests/SerializerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,50 @@ public function testDenormalizeOnNormalizer()
7373
$this->assertTrue($this->serializer->denormalize(json_encode($data), 'stdClass', 'json'));
7474
}
7575

76+
public function testNormalizeWithSupportOnData()
77+
{
78+
$normalizer1 = $this->getMock('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
79+
$normalizer1->method('supportsNormalization')
80+
->willReturnCallback(function ($data, $format) {
81+
return isset($data->test);
82+
});
83+
$normalizer1->method('normalize')->willReturn('test1');
84+
85+
$normalizer2 = $this->getMock('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
86+
$normalizer2->method('supportsNormalization')
87+
->willReturn(true);
88+
$normalizer2->method('normalize')->willReturn('test2');
89+
90+
$serializer = new Serializer(array($normalizer1, $normalizer2));
91+
92+
$data = new \stdClass();
93+
$data->test = true;
94+
$this->assertEquals('test1', $serializer->normalize($data));
95+
96+
$this->assertEquals('test2', $serializer->normalize(new \stdClass()));
97+
}
98+
99+
public function testDenormalizeWithSupportOnData()
100+
{
101+
$denormalizer1 = $this->getMock('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
102+
$denormalizer1->method('supportsDenormalization')
103+
->willReturnCallback(function ($data, $type, $format) {
104+
return isset($data['test1']);
105+
});
106+
$denormalizer1->method('denormalize')->willReturn('test1');
107+
108+
$denormalizer2 = $this->getMock('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
109+
$denormalizer2->method('supportsDenormalization')
110+
->willReturn(true);
111+
$denormalizer2->method('denormalize')->willReturn('test2');
112+
113+
$serializer = new Serializer(array($denormalizer1, $denormalizer2));
114+
115+
$this->assertEquals('test1', $serializer->denormalize(array('test1' => true), 'test'));
116+
117+
$this->assertEquals('test2', $serializer->denormalize(array(), 'test'));
118+
}
119+
76120
public function testSerialize()
77121
{
78122
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));

0 commit comments

Comments
 (0)