Skip to content

fix(symfony): explicitly set the target when mapping entities to resources #7311

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
Jul 25, 2025
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
2 changes: 1 addition & 1 deletion src/State/Processor/ObjectMapperProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
return $this->decorated->process($data, $operation, $uriVariables, $context);
}

return $this->objectMapper->map($this->decorated->process($this->objectMapper->map($data), $operation, $uriVariables, $context));
return $this->objectMapper->map($this->decorated->process($this->objectMapper->map($data), $operation, $uriVariables, $context), $operation->getClass());
}
}
4 changes: 2 additions & 2 deletions src/State/Provider/ObjectMapperProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
}

if ($data instanceof PaginatorInterface) {
$data = new ArrayPaginator(array_map(fn ($v) => $this->objectMapper->map($v), iterator_to_array($data)), 0, \count($data));
$data = new ArrayPaginator(array_map(fn ($v) => $this->objectMapper->map($v, $operation->getClass()), iterator_to_array($data)), 0, \count($data));
} else {
$data = $this->objectMapper->map($data);
$data = $this->objectMapper->map($data, $operation->getClass());
}

$request?->attributes->set('data', $data);
Expand Down
34 changes: 34 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/FirstResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
shortName: 'First',
operations: [new GetCollection()],
stateOptions: new Options(entityClass: SameEntity::class)
)]
#[Map(target: SameEntity::class)]
final class FirstResource
{
#[Map(if: false)]
public ?int $id = null;

public string $name;
}
36 changes: 36 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/SecondResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
shortName: 'Second',
operations: [new GetCollection()],
stateOptions: new Options(entityClass: SameEntity::class)
)]
#[Map(target: SameEntity::class)]
final class SecondResource
{
#[Map(if: false)]
public ?int $id = null;

public string $name;

public string $extra = 'field';
}
48 changes: 48 additions & 0 deletions tests/Fixtures/TestBundle/Entity/SameEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Entity;

use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FirstResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SecondResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ORM\Entity]
#[Map(target: FirstResource::class)]
#[Map(target: SecondResource::class)]
class SameEntity
{
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private ?int $id = null;

#[ORM\Column]
private string $name;

public function getId(): ?int
{
return $this->id;
}

public function setName(string $name): void
{
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}
}
28 changes: 27 additions & 1 deletion tests/Functional/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
namespace ApiPlatform\Tests\Functional;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FirstResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceOdm;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SecondResource;
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MappedDocument;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntity;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
use ApiPlatform\Tests\RecreateSchemaTrait;
use ApiPlatform\Tests\SetupClassResourcesTrait;
use Doctrine\ODM\MongoDB\DocumentManager;
Expand All @@ -33,7 +36,7 @@ final class MappingTest extends ApiTestCase
*/
public static function getResources(): array
{
return [MappedResource::class, MappedResourceOdm::class];
return [MappedResource::class, MappedResourceOdm::class, FirstResource::class, SecondResource::class];
}

public function testShouldMapBetweenResourceAndEntity(): void
Expand Down Expand Up @@ -68,6 +71,29 @@ public function testShouldMapBetweenResourceAndEntity(): void
$this->assertJsonContains(['username' => 'ba zar']);
}

public function testShouldMapToTheCorrectResource(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('MongoDB not tested.');
}

if (!$this->getContainer()->has('object_mapper')) {
$this->markTestSkipped('ObjectMapper not installed');
}

$this->recreateSchema([SameEntity::class]);
$manager = $this->getManager();
$e = new SameEntity();
$e->setName('foo');
$manager->persist($e);
$manager->flush();

self::createClient()->request('GET', '/seconds');
$this->assertJsonContains(['hydra:member' => [
['name' => 'foo', 'extra' => 'field'],
]]);
}

private function loadFixtures(): void
{
$manager = $this->getManager();
Expand Down
Loading