-
-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: the stateOptions::entityClass should be used when present while …
…building Links (#5550) * fix: the stateOptions::entityClass should be used when present while building Links Fixes #5510 * proper fix --------- Co-authored-by: Manuel Rossard <manuel.rossard@u-bordeaux.fr> Co-authored-by: soyuka <soyuka@users.noreply.github.com>
- Loading branch information
1 parent
60082d7
commit f393574
Showing
8 changed files
with
190 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tests/Fixtures/TestBundle/ApiResource/EntityClassAndCustomProviderResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SeparatedEntity; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\EntityClassAndCustomProviderResourceProvider; | ||
|
||
#[ApiResource( | ||
operations: [ | ||
new Get( | ||
uriTemplate: '/entityClassAndCustomProviderResources/{id}', | ||
uriVariables: ['id'] | ||
), | ||
new GetCollection( | ||
uriTemplate: '/entityClassAndCustomProviderResources' | ||
), | ||
], | ||
provider: EntityClassAndCustomProviderResourceProvider::class, | ||
stateOptions: new Options(entityClass: SeparatedEntity::class) | ||
)] | ||
class EntityClassAndCustomProviderResource | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public ?int $id; | ||
|
||
public ?string $value; | ||
} |
82 changes: 82 additions & 0 deletions
82
tests/Fixtures/TestBundle/State/EntityClassAndCustomProviderResourceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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\State; | ||
|
||
use ApiPlatform\Doctrine\Orm\State\CollectionProvider; | ||
use ApiPlatform\Doctrine\Orm\State\ItemProvider; | ||
use ApiPlatform\Doctrine\Orm\State\Options; | ||
use ApiPlatform\Exception\ItemNotFoundException; | ||
use ApiPlatform\Metadata\CollectionOperationInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\EntityClassAndCustomProviderResource; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SeparatedEntity; | ||
|
||
class EntityClassAndCustomProviderResourceProvider implements ProviderInterface | ||
{ | ||
/** | ||
* Should probably be ProviderInterface for both with a binding in services.yaml in a real app. | ||
*/ | ||
public function __construct( | ||
private readonly ItemProvider $itemProvider, | ||
private readonly CollectionProvider $collectionProvider | ||
) { | ||
} | ||
|
||
/** | ||
* @return EntityClassAndCustomProviderResource[]|EntityClassAndCustomProviderResource|null | ||
*/ | ||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
$operation = ($stateOptions = $operation->getStateOptions()) instanceof Options ? $operation->withClass($stateOptions->getEntityClass()) : $operation; | ||
if ($operation instanceof CollectionOperationInterface) { | ||
$data = $this->collectionProvider->provide( | ||
$operation, | ||
$uriVariables, | ||
$context); | ||
|
||
$processed = []; | ||
|
||
foreach ($data as $item) { | ||
$processed[] = $this->transform($item); | ||
} | ||
|
||
return $processed; | ||
} | ||
|
||
$data = $this->itemProvider->provide( | ||
$operation, | ||
$uriVariables, | ||
$context | ||
); | ||
|
||
if (null === $data) { | ||
throw new ItemNotFoundException(); | ||
} | ||
|
||
return $this->transform($data); | ||
} | ||
|
||
/** | ||
* Would do more in a real app... | ||
*/ | ||
private function transform(SeparatedEntity $data): EntityClassAndCustomProviderResource | ||
{ | ||
$resource = new EntityClassAndCustomProviderResource(); | ||
$resource->id = $data->id; | ||
$resource->value = $data->value; | ||
|
||
return $resource; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters