Skip to content

Commit

Permalink
fix(doctrine): stateOptions force resource class on collection (#6255)
Browse files Browse the repository at this point in the history
fixes #6039
  • Loading branch information
soyuka committed Mar 29, 2024
1 parent 4adc075 commit db50a46
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
12 changes: 12 additions & 0 deletions features/doctrine/issue6039/entity_class_option.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: Test entity class option on collections
In order to retrieve a collections of resources mapped to a DTO automatically
As a client software developer

@createSchema
@!mongodb
Scenario: Get collection
Given there are issue6039 users
And I add "Accept" header equal to "application/ld+json"
When I send a "GET" request to "/issue6039_user_apis"
Then the response status code should be 200
And the JSON node "hydra:member[0].bar" should not exist
4 changes: 4 additions & 0 deletions src/Serializer/AbstractCollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public function normalize(mixed $object, ?string $format = null, array $context
$paginationData = $this->getPaginationData($object, $collectionContext);

$childContext = $this->createOperationContext($collectionContext, $resourceClass);
if (isset($collectionContext['force_resource_class'])) {
$childContext['force_resource_class'] = $collectionContext['force_resource_class'];
}

$itemsData = $this->getItemsData($object, $format, $childContext);

return array_merge_recursive($data, $paginationData, $itemsData);
Expand Down
17 changes: 17 additions & 0 deletions tests/Behat/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5722\Event;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5722\ItemLog;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5735\Group;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6039\Issue6039EntityUser;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\LinkHandledDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MultiRelationsDummy;
Expand Down Expand Up @@ -2270,6 +2271,22 @@ public function thereIsADummyEntityWithAMappedSuperclass(): void
$this->manager->flush();
}

/**
* @Given there are issue6039 users
*/
public function thereAreIssue6039Users(): void
{
$entity = new Issue6039EntityUser();
$entity->name = 'test';
$entity->bar = 'test';
$this->manager->persist($entity);
$entity = new Issue6039EntityUser();
$entity->name = 'test2';
$entity->bar = 'test';
$this->manager->persist($entity);
$this->manager->flush();
}

private function isOrm(): bool
{
return null !== $this->schemaTool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\RelatedDummy;

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

public string $name;

/**
* @var RelatedDummy[]
*/
public array $relatedDummies;
}
25 changes: 25 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue6039/UserApi.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\Fixtures\TestBundle\ApiResource\Issue6039;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6039\Issue6039EntityUser;

#[GetCollection(shortName: 'Issue6039UserApi', stateOptions: new Options(entityClass: Issue6039EntityUser::class))]
class UserApi
{
public string $id;
public string $name;
}
31 changes: 31 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Issue6039/Issue6039EntityUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\Issue6039;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Issue6039EntityUser
{
#[ORM\Column(type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
public ?int $id = null;

#[ORM\Column]
public string $name;

#[ORM\Column]
public string $bar;
}

0 comments on commit db50a46

Please sign in to comment.