Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4816b60

Browse filesBrowse files
committed
[Serializer] fix denormalization of string-arrays with only one element #33731
1 parent ad89564 commit 4816b60
Copy full SHA for 4816b60

File tree

2 files changed

+80
-0
lines changed
Filter options

2 files changed

+80
-0
lines changed

‎src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma
257257
if (null !== $collectionKeyType = $type->getCollectionKeyType()) {
258258
$context['key_type'] = $collectionKeyType;
259259
}
260+
} elseif ($type->isCollection() && null !== $type->getCollectionValueType()) {
261+
if ('xml' === $format && !\is_array($data)) {
262+
$data = [$data];
263+
}
264+
$builtinType = $type->getBuiltinType();
265+
$class = $type->getClassName();
260266
} else {
261267
$builtinType = $type->getBuiltinType();
262268
$class = $type->getClassName();

‎src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php
+74Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,74 @@ private function getDenormalizerForDummyCollection()
143143
return $denormalizer;
144144
}
145145

146+
public function testDenormalizeStringCollectionDecodedFromXmlWithOneChild()
147+
{
148+
$denormalizer = $this->getDenormalizerForStringCollection();
149+
150+
// if an xml-node can have children which should be deserialized as string[]
151+
// and only one child exists
152+
$stringCollection = $denormalizer->denormalize(
153+
[
154+
'children' => 'foo',
155+
],
156+
StringCollection::class,
157+
'xml'
158+
);
159+
160+
$this->assertInstanceOf(StringCollection::class, $stringCollection);
161+
$this->assertIsArray($stringCollection->children);
162+
$this->assertCount(1, $stringCollection->children);
163+
$this->assertEquals('foo', $stringCollection->children[0]);
164+
}
165+
166+
public function testDenormalizeStringCollectionDecodedFromXmlWithTwoChildren()
167+
{
168+
$denormalizer = $this->getDenormalizerForStringCollection();
169+
170+
// if an xml-node can have children which should be deserialized as string[]
171+
// and only one child exists
172+
$stringCollection = $denormalizer->denormalize(
173+
[
174+
'children' => ['foo', 'bar'],
175+
],
176+
StringCollection::class,
177+
'xml'
178+
);
179+
180+
$this->assertInstanceOf(StringCollection::class, $stringCollection);
181+
$this->assertIsArray($stringCollection->children);
182+
$this->assertCount(2, $stringCollection->children);
183+
$this->assertEquals('foo', $stringCollection->children[0]);
184+
$this->assertEquals('bar', $stringCollection->children[1]);
185+
}
186+
187+
private function getDenormalizerForStringCollection()
188+
{
189+
$extractor = $this->getMockBuilder(PhpDocExtractor::class)->getMock();
190+
$extractor->method('getTypes')
191+
->will($this->onConsecutiveCalls(
192+
[
193+
new Type(
194+
'array',
195+
false,
196+
null,
197+
true,
198+
new Type('int'),
199+
new Type('string')
200+
),
201+
],
202+
null
203+
));
204+
205+
$denormalizer = new AbstractObjectNormalizerCollectionDummy(null, null, $extractor);
206+
$arrayDenormalizer = new ArrayDenormalizerDummy();
207+
$serializer = new SerializerCollectionDummy([$arrayDenormalizer, $denormalizer]);
208+
$arrayDenormalizer->setSerializer($serializer);
209+
$denormalizer->setSerializer($serializer);
210+
211+
return $denormalizer;
212+
}
213+
146214
/**
147215
* Test that additional attributes throw an exception if no metadata factory is specified.
148216
*/
@@ -212,6 +280,12 @@ protected function setAttributeValue($object, $attribute, $value, $format = null
212280
}
213281
}
214282

283+
class StringCollection
284+
{
285+
/** @var string[] */
286+
public $children;
287+
}
288+
215289
class DummyCollection
216290
{
217291
/** @var DummyChild[] */

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.