-
-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: search on nested sub-entity that doesn't use "id" as its ORM ide…
…ntifier
- Loading branch information
Showing
12 changed files
with
344 additions
and
4 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
41 changes: 41 additions & 0 deletions
41
tests/Fixtures/TestBundle/ApiResource/Issue5605/MainResource.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,41 @@ | ||
<?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\Issue5605; | ||
|
||
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; | ||
use ApiPlatform\Doctrine\Orm\State\Options; | ||
use ApiPlatform\Metadata\ApiFilter; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyWithSubEntity; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\Issue5605\MainResourceProvider; | ||
|
||
#[ApiResource( | ||
operations : [ | ||
new Get(uriTemplate: '/dummy_with_subresource/{id}', uriVariables: ['id']), | ||
new GetCollection(uriTemplate: '/dummy_with_subresource') | ||
], | ||
provider : MainResourceProvider::class, | ||
stateOptions: new Options(entityClass: DummyWithSubEntity::class) | ||
)] | ||
#[ApiFilter(SearchFilter::class, properties: ['subEntity'])] | ||
class MainResource | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public int $id; | ||
public string $name; | ||
public SubResource $subResource; | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/Fixtures/TestBundle/ApiResource/Issue5605/SubResource.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,39 @@ | ||
<?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\Issue5605; | ||
|
||
use ApiPlatform\Doctrine\Orm\State\Options; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummySubEntity; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\Issue5605\SubResourceProvider; | ||
|
||
#[ApiResource( | ||
operations : [ | ||
new Get( | ||
uriTemplate: '/dummy_subresource/{strId}', | ||
uriVariables: ['strId'] | ||
) | ||
], | ||
provider: SubResourceProvider::class, | ||
stateOptions: new Options(entityClass: DummySubEntity::class) | ||
)] | ||
class SubResource | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public string $strId; | ||
|
||
public string $name; | ||
} |
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,61 @@ | ||
<?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 Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class DummySubEntity | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'string')] | ||
private string $strId; | ||
|
||
#[ORM\Column] | ||
private string $name; | ||
|
||
#[ORM\OneToOne(inversedBy: 'subEntity', cascade: ['persist'])] | ||
private ?DummyWithSubEntity $mainEntity = null; | ||
|
||
public function __construct($strId, $name) | ||
{ | ||
$this->strId = $strId; | ||
$this->name = $name; | ||
} | ||
|
||
public function getStrId(): string | ||
{ | ||
return $this->strId; | ||
} | ||
|
||
public function getMainEntity(): ?DummyWithSubEntity | ||
{ | ||
return $this->mainEntity; | ||
} | ||
|
||
public function setMainEntity(DummyWithSubEntity $mainEntity): void | ||
{ | ||
$this->mainEntity = $mainEntity; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
} |
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,60 @@ | ||
<?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 Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class DummyWithSubEntity | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private int $id; | ||
|
||
#[ORM\Column] | ||
private string $name; | ||
|
||
#[ORM\OneToOne(mappedBy: 'mainEntity', cascade: ['persist'], fetch: 'EAGER')] | ||
private ?DummySubEntity $subEntity = null; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getSubEntity(): ?DummySubEntity | ||
{ | ||
return $this->subEntity; | ||
} | ||
|
||
public function setSubEntity(?DummySubEntity $subEntity): void | ||
{ | ||
if (null !== $subEntity && $subEntity->getMainEntity() !== $this) { | ||
$subEntity->setMainEntity($this); | ||
} | ||
|
||
$this->subEntity = $subEntity; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
tests/Fixtures/TestBundle/State/Issue5605/MainResourceProvider.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,56 @@ | ||
<?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\Issue5605; | ||
|
||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5605\MainResource; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5605\SubResource; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyWithSubEntity; | ||
|
||
class MainResourceProvider implements ProviderInterface | ||
{ | ||
public function __construct(private readonly ProviderInterface $itemProvider, private readonly ProviderInterface $collectionProvider) | ||
{ | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
if ($operation instanceof Get) { | ||
/** | ||
* @var DummyWithSubEntity $entity | ||
*/ | ||
$entity = $this->itemProvider->provide($operation, $uriVariables, $context); | ||
return $this->getResource($entity); | ||
} | ||
$resources = []; | ||
$entities = $this->collectionProvider->provide($operation, $uriVariables, $context); | ||
foreach($entities as $entity) { | ||
$resources[] = $this->getResource($entity); | ||
} | ||
return $resources; | ||
} | ||
|
||
protected function getResource(DummyWithSubEntity $entity): MainResource | ||
{ | ||
$resource = new MainResource(); | ||
$resource->name = $entity->getName(); | ||
$resource->id = $entity->getId(); | ||
$resource->subResource = new SubResource(); | ||
$resource->subResource->name = $entity->getSubEntity()->getName(); | ||
$resource->subResource->strId = $entity->getSubEntity()->getStrId(); | ||
return $resource; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/Fixtures/TestBundle/State/Issue5605/SubResourceProvider.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,39 @@ | ||
<?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\Issue5605; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue5605\SubResource; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummySubEntity; | ||
|
||
class SubResourceProvider implements ProviderInterface | ||
{ | ||
|
||
public function __construct(private readonly ProviderInterface $itemProvider) | ||
{ | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
/** | ||
* @var DummySubEntity $entity | ||
*/ | ||
$entity = $this->itemProvider->provide($operation, $uriVariables, $context); | ||
$resource = new SubResource(); | ||
$resource->strId = $entity->getStrId(); | ||
$resource->name = $entity->getName(); | ||
return $resource; | ||
} | ||
} |
Oops, something went wrong.