Skip to content

Commit 88ad9e4

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: Typo fix [2.3] Static Code Analysis for Components Added support \IteratorAggregate for UniqueEntityValidator Update AbstractChoiceListTest.php Fix #17306 Paths with % in it are note allowed (like urlencoded) Use proper class to fetch $versionStrategy property Added sort order SORT_STRING for params in UriSigner Remove normalizer cache in Serializer class
2 parents 20e1eb4 + 3225b57 commit 88ad9e4

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

Serializer.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,10 @@ private function getNormalizer($data, $format)
187187
{
188188
if ($isObject = is_object($data)) {
189189
$class = get_class($data);
190-
if (isset($this->normalizerCache[$class][$format])) {
191-
return $this->normalizerCache[$class][$format];
192-
}
193190
}
194191

195192
foreach ($this->normalizers as $normalizer) {
196193
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
197-
if ($isObject) {
198-
$this->normalizerCache[$class][$format] = $normalizer;
199-
}
200-
201194
return $normalizer;
202195
}
203196
}
@@ -214,14 +207,8 @@ private function getNormalizer($data, $format)
214207
*/
215208
private function getDenormalizer($data, $class, $format)
216209
{
217-
if (isset($this->denormalizerCache[$class][$format])) {
218-
return $this->denormalizerCache[$class][$format];
219-
}
220-
221210
foreach ($this->normalizers as $normalizer) {
222211
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $class, $format)) {
223-
$this->denormalizerCache[$class][$format] = $normalizer;
224-
225212
return $normalizer;
226213
}
227214
}
@@ -264,6 +251,7 @@ private function normalizeObject($object, $format, array $context = array())
264251
if ($normalizer = $this->getNormalizer($object, $format)) {
265252
return $normalizer->normalize($object, $format, $context);
266253
}
254+
267255
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($object)));
268256
}
269257

@@ -289,6 +277,15 @@ private function denormalizeObject($data, $class, $format, array $context = arra
289277
if ($normalizer = $this->getDenormalizer($data, $class, $format)) {
290278
return $normalizer->denormalize($data, $class, $format, $context);
291279
}
280+
281+
foreach ($this->normalizers as $normalizer) {
282+
if ($normalizer instanceof DenormalizerInterface
283+
&& $normalizer->supportsDenormalization($data, $class, $format)) {
284+
285+
return $normalizer->denormalize($data, $class, $format, $context);
286+
}
287+
}
288+
292289
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
293290
}
294291

Tests/SerializerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,50 @@ public function testCustomNormalizerCanNormalizeCollectionsAndScalar()
9696
$this->assertNull($serializer->normalize('test'));
9797
}
9898

99+
public function testNormalizeWithSupportOnData()
100+
{
101+
$normalizer1 = $this->getMock('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
102+
$normalizer1->method('supportsNormalization')
103+
->willReturnCallback(function ($data, $format) {
104+
return isset($data->test);
105+
});
106+
$normalizer1->method('normalize')->willReturn('test1');
107+
108+
$normalizer2 = $this->getMock('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
109+
$normalizer2->method('supportsNormalization')
110+
->willReturn(true);
111+
$normalizer2->method('normalize')->willReturn('test2');
112+
113+
$serializer = new Serializer(array($normalizer1, $normalizer2));
114+
115+
$data = new \stdClass();
116+
$data->test = true;
117+
$this->assertEquals('test1', $serializer->normalize($data));
118+
119+
$this->assertEquals('test2', $serializer->normalize(new \stdClass()));
120+
}
121+
122+
public function testDenormalizeWithSupportOnData()
123+
{
124+
$denormalizer1 = $this->getMock('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
125+
$denormalizer1->method('supportsDenormalization')
126+
->willReturnCallback(function ($data, $type, $format) {
127+
return isset($data['test1']);
128+
});
129+
$denormalizer1->method('denormalize')->willReturn('test1');
130+
131+
$denormalizer2 = $this->getMock('Symfony\Component\Serializer\Normalizer\DenormalizerInterface');
132+
$denormalizer2->method('supportsDenormalization')
133+
->willReturn(true);
134+
$denormalizer2->method('denormalize')->willReturn('test2');
135+
136+
$serializer = new Serializer(array($denormalizer1, $denormalizer2));
137+
138+
$this->assertEquals('test1', $serializer->denormalize(array('test1' => true), 'test'));
139+
140+
$this->assertEquals('test2', $serializer->denormalize(array(), 'test'));
141+
}
142+
99143
public function testSerialize()
100144
{
101145
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));

0 commit comments

Comments
 (0)