Skip to content

[Serializer] Allow multi-dimenstion object array in AbstractObjectNormalizer #32832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,26 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
if (null !== $collectionKeyType = $type->getCollectionKeyType()) {
$context['key_type'] = $collectionKeyType;
}
} elseif ($type->isCollection() && null !== ($collectionValueType = $type->getCollectionValueType()) && Type::BUILTIN_TYPE_ARRAY === $collectionValueType->getBuiltinType()) {
// get inner type for any nested array
$innerType = $collectionValueType;

// note that it will break for any other builtinType
$dimensions = '[]';
while (null !== $innerType->getCollectionValueType() && Type::BUILTIN_TYPE_ARRAY === $innerType->getBuiltinType()) {
$dimensions .= '[]';
$innerType = $innerType->getCollectionValueType();
}

if (null !== $innerType->getClassName()) {
// the builtinType is the inner one and the class is the class followed by []...[]
$builtinType = $innerType->getBuiltinType();
$class = $innerType->getClassName().$dimensions;
} else {
// default fallback (keep it as array)
$builtinType = $type->getBuiltinType();
$class = $type->getClassName();
}
} else {
$builtinType = $type->getBuiltinType();
$class = $type->getClassName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyChild;
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
Expand Down Expand Up @@ -407,6 +408,52 @@ public function testInheritedPropertiesSupport()
{
$this->assertTrue($this->normalizer->supportsNormalization(new PropertyChildDummy()));
}

public function testMultiDimensionObject()
{
$normalizer = $this->getDenormalizerForTypeEnforcement();
$root = $normalizer->denormalize([
'children' => [[
['foo' => 'one', 'bar' => 'two'],
['foo' => 'three', 'bar' => 'four'],
]],
'grandChildren' => [[[
['foo' => 'five', 'bar' => 'six'],
['foo' => 'seven', 'bar' => 'eight'],
]]],
'intMatrix' => [
[0, 1, 2],
[3, 4, 5],
],
],
RootDummy::class,
'any'
);
$this->assertEquals(\get_class($root), RootDummy::class);

// children (two dimension array)
$this->assertCount(1, $root->children);
$this->assertCount(2, $root->children[0]);
$firstChild = $root->children[0][0];
$this->assertInstanceOf(Dummy::class, $firstChild);
$this->assertSame('one', $firstChild->foo);
$this->assertSame('two', $firstChild->bar);

// grand children (three dimension array)
$this->assertCount(1, $root->grandChildren);
$this->assertCount(1, $root->grandChildren[0]);
$this->assertCount(2, $root->grandChildren[0][0]);
$firstGrandChild = $root->grandChildren[0][0][0];
$this->assertInstanceOf(Dummy::class, $firstGrandChild);
$this->assertSame('five', $firstGrandChild->foo);
$this->assertSame('six', $firstGrandChild->bar);

// int matrix
$this->assertSame([
[0, 1, 2],
[3, 4, 5],
], $root->intMatrix);
}
}

class PropertyDummy
Expand Down Expand Up @@ -472,3 +519,34 @@ class PropertyParentDummy
class PropertyChildDummy extends PropertyParentDummy
{
}

class RootDummy
{
public $children;
public $grandChildren;
public $intMatrix;

/**
* @return Dummy[][]
*/
public function getChildren(): array
{
return $this->children;
}

/**
* @return Dummy[][][]
*/
public function getGrandChildren()
{
return $this->grandChildren;
}

/**
* @return array
*/
public function getIntMatrix()
{
return $this->intMatrix;
}
}