Skip to content

Commit 874e4d6

Browse files
authored
fix(serializer): collection property in an output dto (#6239)
fixes #6211
1 parent 5a2600f commit 874e4d6

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/Serializer/AbstractItemNormalizer.php

+4
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,10 @@ protected function getAttributeValue(object $object, string $attribute, ?string
732732
}
733733

734734
if ('array' === $type->getBuiltinType()) {
735+
if ($className = ($type->getCollectionValueTypes()[0] ?? null)?->getClassName()) {
736+
$context = $this->createOperationContext($context, $className);
737+
}
738+
735739
$childContext = $this->createChildContext($context, $attribute, $format);
736740
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;
737741

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6211;
15+
16+
use ApiPlatform\Metadata\Get;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\ArrayPropertyDto;
18+
19+
#[Get(provider: [ArrayPropertyDtoOperation::class, 'provide'], output: ArrayPropertyDto::class)]
20+
class ArrayPropertyDtoOperation
21+
{
22+
public static function provide(): ArrayPropertyDto
23+
{
24+
$d = new ArrayPropertyDto();
25+
$d->name = 'test';
26+
$c = new ArrayPropertyDto();
27+
$c->name = 'test2';
28+
$d->greetings = [$c];
29+
30+
return $d;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Dto;
15+
16+
final class ArrayPropertyDto
17+
{
18+
public string $name = '';
19+
20+
/**
21+
* @var array<ArrayPropertyDto>
22+
*/
23+
public array $greetings = [];
24+
}

tests/Functional/ArrayDtoTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Functional;
15+
16+
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
18+
final class ArrayDtoTest extends ApiTestCase
19+
{
20+
public function testWithGroupFilter(): void
21+
{
22+
$response = self::createClient()->request('GET', '/array_property_dto_operations');
23+
$this->assertArraySubset(['name' => 'test', 'greetings' => [['name' => 'test2']]], $response->toArray());
24+
}
25+
}

0 commit comments

Comments
 (0)