Skip to content

Commit db50a46

Browse files
authored
fix(doctrine): stateOptions force resource class on collection (#6255)
fixes #6039
1 parent 4adc075 commit db50a46

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Feature: Test entity class option on collections
2+
In order to retrieve a collections of resources mapped to a DTO automatically
3+
As a client software developer
4+
5+
@createSchema
6+
@!mongodb
7+
Scenario: Get collection
8+
Given there are issue6039 users
9+
And I add "Accept" header equal to "application/ld+json"
10+
When I send a "GET" request to "/issue6039_user_apis"
11+
Then the response status code should be 200
12+
And the JSON node "hydra:member[0].bar" should not exist

src/Serializer/AbstractCollectionNormalizer.php

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public function normalize(mixed $object, ?string $format = null, array $context
103103
$paginationData = $this->getPaginationData($object, $collectionContext);
104104

105105
$childContext = $this->createOperationContext($collectionContext, $resourceClass);
106+
if (isset($collectionContext['force_resource_class'])) {
107+
$childContext['force_resource_class'] = $collectionContext['force_resource_class'];
108+
}
109+
106110
$itemsData = $this->getItemsData($object, $format, $childContext);
107111

108112
return array_merge_recursive($data, $paginationData, $itemsData);

tests/Behat/DoctrineContext.php

+17
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5722\Event;
156156
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5722\ItemLog;
157157
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5735\Group;
158+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6039\Issue6039EntityUser;
158159
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\LinkHandledDummy;
159160
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
160161
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MultiRelationsDummy;
@@ -2270,6 +2271,22 @@ public function thereIsADummyEntityWithAMappedSuperclass(): void
22702271
$this->manager->flush();
22712272
}
22722273

2274+
/**
2275+
* @Given there are issue6039 users
2276+
*/
2277+
public function thereAreIssue6039Users(): void
2278+
{
2279+
$entity = new Issue6039EntityUser();
2280+
$entity->name = 'test';
2281+
$entity->bar = 'test';
2282+
$this->manager->persist($entity);
2283+
$entity = new Issue6039EntityUser();
2284+
$entity->name = 'test2';
2285+
$entity->bar = 'test';
2286+
$this->manager->persist($entity);
2287+
$this->manager->flush();
2288+
}
2289+
22732290
private function isOrm(): bool
22742291
{
22752292
return null !== $this->schemaTool;

tests/Fixtures/TestBundle/ApiResource/Issue5648/DummyResource.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
use ApiPlatform\Metadata\GetCollection;
2222
use ApiPlatform\Metadata\Link;
2323
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
24+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
2425

2526
#[ApiResource(
2627
operations: [
27-
new GetCollection(uriTemplate: '/dummy_resource_with_custom_filter', itemUriTemplate: '/dummy_resource_with_custom_filter/{id}'),
28+
new GetCollection(uriTemplate: '/dummy_resource_with_custom_filter{._format}', itemUriTemplate: '/dummy_resource_with_custom_filter/{id}'),
2829
new Get(uriTemplate: '/dummy_resource_with_custom_filter/{id}', uriVariables: ['id' => new Link(fromClass: Dummy::class)]),
2930
],
3031
stateOptions: new Options(entityClass: Dummy::class)
@@ -37,5 +38,8 @@ class DummyResource
3738

3839
public string $name;
3940

41+
/**
42+
* @var RelatedDummy[]
43+
*/
4044
public array $relatedDummies;
4145
}
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\Fixtures\TestBundle\ApiResource\Issue6039;
15+
16+
use ApiPlatform\Doctrine\Orm\State\Options;
17+
use ApiPlatform\Metadata\GetCollection;
18+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6039\Issue6039EntityUser;
19+
20+
#[GetCollection(shortName: 'Issue6039UserApi', stateOptions: new Options(entityClass: Issue6039EntityUser::class))]
21+
class UserApi
22+
{
23+
public string $id;
24+
public string $name;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Entity\Issue6039;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
18+
#[ORM\Entity]
19+
class Issue6039EntityUser
20+
{
21+
#[ORM\Column(type: 'integer')]
22+
#[ORM\Id]
23+
#[ORM\GeneratedValue(strategy: 'AUTO')]
24+
public ?int $id = null;
25+
26+
#[ORM\Column]
27+
public string $name;
28+
29+
#[ORM\Column]
30+
public string $bar;
31+
}

0 commit comments

Comments
 (0)