Skip to content

Commit 3225b57

Browse files
committed
Merge branch '2.3' into 2.7
* 2.3: Typo fix [2.3] Static Code Analysis for Components Added support \IteratorAggregate for UniqueEntityValidator Fix #17306 Paths with % in it are note allowed (like urlencoded) Added sort order SORT_STRING for params in UriSigner Remove normalizer cache in Serializer class
2 parents fea7673 + 61505e6 commit 3225b57

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
@@ -93,6 +93,50 @@ public function testCustomNormalizerCanNormalizeCollectionsAndScalar()
9393
$this->assertNull($serializer->normalize('test'));
9494
}
9595

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

0 commit comments

Comments
 (0)