Skip to content

fix(serializer): collection property in an output dto #6239

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
merged 1 commit into from
Mar 22, 2024
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
4 changes: 4 additions & 0 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ protected function getAttributeValue(object $object, string $attribute, ?string
}

if ('array' === $type->getBuiltinType()) {
if ($className = ($type->getCollectionValueTypes()[0] ?? null)?->getClassName()) {
$context = $this->createOperationContext($context, $className);
}

$childContext = $this->createChildContext($context, $attribute, $format);
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6211;

use ApiPlatform\Metadata\Get;
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\ArrayPropertyDto;

#[Get(provider: [ArrayPropertyDtoOperation::class, 'provide'], output: ArrayPropertyDto::class)]
class ArrayPropertyDtoOperation
{
public static function provide(): ArrayPropertyDto
{
$d = new ArrayPropertyDto();
$d->name = 'test';
$c = new ArrayPropertyDto();
$c->name = 'test2';
$d->greetings = [$c];

return $d;
}
}
24 changes: 24 additions & 0 deletions tests/Fixtures/TestBundle/Dto/ArrayPropertyDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Dto;

final class ArrayPropertyDto
{
public string $name = '';

/**
* @var array<ArrayPropertyDto>
*/
public array $greetings = [];
}
25 changes: 25 additions & 0 deletions tests/Functional/ArrayDtoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Functional;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;

final class ArrayDtoTest extends ApiTestCase
{
public function testWithGroupFilter(): void
{
$response = self::createClient()->request('GET', '/array_property_dto_operations');
$this->assertArraySubset(['name' => 'test', 'greetings' => [['name' => 'test2']]], $response->toArray());
}
}