Skip to content

Commit 02a7649

Browse files
authored
fix(symfony): explicitly set the target class when mapping entities to resources (#7311)
1 parent 9e9cf64 commit 02a7649

File tree

6 files changed

+148
-4
lines changed

6 files changed

+148
-4
lines changed

src/State/Processor/ObjectMapperProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
4242
return $this->decorated->process($data, $operation, $uriVariables, $context);
4343
}
4444

45-
return $this->objectMapper->map($this->decorated->process($this->objectMapper->map($data), $operation, $uriVariables, $context));
45+
return $this->objectMapper->map($this->decorated->process($this->objectMapper->map($data), $operation, $uriVariables, $context), $operation->getClass());
4646
}
4747
}

src/State/Provider/ObjectMapperProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6464
}
6565

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

7272
$request?->attributes->set('data', $data);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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;
15+
16+
use ApiPlatform\Doctrine\Orm\State\Options;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
20+
use Symfony\Component\ObjectMapper\Attribute\Map;
21+
22+
#[ApiResource(
23+
shortName: 'First',
24+
operations: [new GetCollection()],
25+
stateOptions: new Options(entityClass: SameEntity::class)
26+
)]
27+
#[Map(target: SameEntity::class)]
28+
final class FirstResource
29+
{
30+
#[Map(if: false)]
31+
public ?int $id = null;
32+
33+
public string $name;
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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;
15+
16+
use ApiPlatform\Doctrine\Orm\State\Options;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
20+
use Symfony\Component\ObjectMapper\Attribute\Map;
21+
22+
#[ApiResource(
23+
shortName: 'Second',
24+
operations: [new GetCollection()],
25+
stateOptions: new Options(entityClass: SameEntity::class)
26+
)]
27+
#[Map(target: SameEntity::class)]
28+
final class SecondResource
29+
{
30+
#[Map(if: false)]
31+
public ?int $id = null;
32+
33+
public string $name;
34+
35+
public string $extra = 'field';
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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\Entity;
15+
16+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FirstResource;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SecondResource;
18+
use Doctrine\ORM\Mapping as ORM;
19+
use Symfony\Component\ObjectMapper\Attribute\Map;
20+
21+
#[ORM\Entity]
22+
#[Map(target: FirstResource::class)]
23+
#[Map(target: SecondResource::class)]
24+
class SameEntity
25+
{
26+
#[ORM\Column(type: 'integer')]
27+
#[ORM\Id]
28+
#[ORM\GeneratedValue(strategy: 'AUTO')]
29+
private ?int $id = null;
30+
31+
#[ORM\Column]
32+
private string $name;
33+
34+
public function getId(): ?int
35+
{
36+
return $this->id;
37+
}
38+
39+
public function setName(string $name): void
40+
{
41+
$this->name = $name;
42+
}
43+
44+
public function getName(): string
45+
{
46+
return $this->name;
47+
}
48+
}

tests/Functional/MappingTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
namespace ApiPlatform\Tests\Functional;
1515

1616
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FirstResource;
1718
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResource;
1819
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceOdm;
20+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SecondResource;
1921
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MappedDocument;
2022
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntity;
23+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
2124
use ApiPlatform\Tests\RecreateSchemaTrait;
2225
use ApiPlatform\Tests\SetupClassResourcesTrait;
2326
use Doctrine\ODM\MongoDB\DocumentManager;
@@ -33,7 +36,7 @@ final class MappingTest extends ApiTestCase
3336
*/
3437
public static function getResources(): array
3538
{
36-
return [MappedResource::class, MappedResourceOdm::class];
39+
return [MappedResource::class, MappedResourceOdm::class, FirstResource::class, SecondResource::class];
3740
}
3841

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

74+
public function testShouldMapToTheCorrectResource(): void
75+
{
76+
if ($this->isMongoDB()) {
77+
$this->markTestSkipped('MongoDB not tested.');
78+
}
79+
80+
if (!$this->getContainer()->has('object_mapper')) {
81+
$this->markTestSkipped('ObjectMapper not installed');
82+
}
83+
84+
$this->recreateSchema([SameEntity::class]);
85+
$manager = $this->getManager();
86+
$e = new SameEntity();
87+
$e->setName('foo');
88+
$manager->persist($e);
89+
$manager->flush();
90+
91+
self::createClient()->request('GET', '/seconds');
92+
$this->assertJsonContains(['hydra:member' => [
93+
['name' => 'foo', 'extra' => 'field'],
94+
]]);
95+
}
96+
7197
private function loadFixtures(): void
7298
{
7399
$manager = $this->getManager();

0 commit comments

Comments
 (0)