Skip to content

Commit 8e343fe

Browse files
authored
Merge pull request #2859 from karser/2.4-collection-denormalization-fix
Fixed denormalization of a constructor argument which is a collection of non-resources
2 parents b1815d6 + 4e455bc commit 8e343fe

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

features/serializer/deserialize_objects_using_constructor.feature

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ Feature: Resource with constructor deserializable
1010
"""
1111
{
1212
"foo": "hello",
13-
"bar": "world"
13+
"bar": "world",
14+
"items": [
15+
{
16+
"foo": "bar"
17+
}
18+
]
1419
}
1520
"""
1621
Then the response status code should be 201
@@ -25,7 +30,11 @@ Feature: Resource with constructor deserializable
2530
"id": 1,
2631
"foo": "hello",
2732
"bar": "world",
33+
"items": [
34+
{
35+
"foo": "bar"
36+
}
37+
],
2838
"baz": null
2939
}
3040
"""
31-

src/Serializer/AbstractItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ private function createAttributeValue($attribute, $value, $format = null, array
677677

678678
unset($context['resource_class']);
679679

680-
return $this->serializer->denormalize($value, $className, $format, $context);
680+
return $this->serializer->denormalize($value, $className.'[]', $format, $context);
681681
}
682682

683683
if (null !== $className = $type->getClassName()) {

tests/Fixtures/TestBundle/Document/DummyEntityWithConstructor.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Document;
1515

1616
use ApiPlatform\Core\Annotation\ApiResource;
17+
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithoutConstructor;
1718
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
1819
use Symfony\Component\Serializer\Annotation\Groups;
1920

@@ -62,10 +63,19 @@ class DummyEntityWithConstructor
6263
*/
6364
private $baz;
6465

65-
public function __construct(string $foo, string $bar)
66+
/**
67+
* @var DummyObjectWithoutConstructor[]
68+
*/
69+
private $items;
70+
71+
/**
72+
* @param DummyObjectWithoutConstructor[] $items
73+
*/
74+
public function __construct(string $foo, string $bar, array $items)
6675
{
6776
$this->foo = $foo;
6877
$this->bar = $bar;
78+
$this->items = $items;
6979
}
7080

7181
public function getId(): int
@@ -83,6 +93,14 @@ public function getBar(): string
8393
return $this->bar;
8494
}
8595

96+
/**
97+
* @return DummyObjectWithoutConstructor[]
98+
*/
99+
public function getItems(): array
100+
{
101+
return $this->items;
102+
}
103+
86104
/**
87105
* @return string
88106
*/

tests/Fixtures/TestBundle/Entity/DummyEntityWithConstructor.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
1515

1616
use ApiPlatform\Core\Annotation\ApiResource;
17+
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithoutConstructor;
1718
use Doctrine\ORM\Mapping as ORM;
1819
use Symfony\Component\Serializer\Annotation\Groups;
1920

@@ -64,10 +65,19 @@ class DummyEntityWithConstructor
6465
*/
6566
private $baz;
6667

67-
public function __construct(string $foo, string $bar)
68+
/**
69+
* @var DummyObjectWithoutConstructor[]
70+
*/
71+
private $items;
72+
73+
/**
74+
* @param DummyObjectWithoutConstructor[] $items
75+
*/
76+
public function __construct(string $foo, string $bar, array $items)
6877
{
6978
$this->foo = $foo;
7079
$this->bar = $bar;
80+
$this->items = $items;
7181
}
7282

7383
public function getId(): int
@@ -85,6 +95,14 @@ public function getBar(): string
8595
return $this->bar;
8696
}
8797

98+
/**
99+
* @return DummyObjectWithoutConstructor[]
100+
*/
101+
public function getItems(): array
102+
{
103+
return $this->items;
104+
}
105+
88106
/**
89107
* @return string
90108
*/

0 commit comments

Comments
 (0)