-
-
Notifications
You must be signed in to change notification settings - Fork 894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(doctrine): resource_class from context instead of entity class #6592
Open
KaiGrassnick
wants to merge
3
commits into
api-platform:3.3
Choose a base branch
from
KaiGrassnick:fix/3.3-links-handler-graphql-query-with-dtos
base: 3.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 40 additions & 0 deletions
40
tests/Fixtures/TestBundle/ApiResource/Issue6590/OrmBarResource.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,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Issue6590; | ||
|
||
use ApiPlatform\Doctrine\Orm\State\Options; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\GraphQl\Query; | ||
use ApiPlatform\Metadata\GraphQl\QueryCollection; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6590\Bar; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\Issue6590\BarResourceProvider; | ||
|
||
#[ApiResource( | ||
shortName: 'Issue6590OrmBar', | ||
operations: [], | ||
graphQlOperations: [ | ||
new Query(), | ||
new QueryCollection(), | ||
], | ||
provider: BarResourceProvider::class, | ||
stateOptions: new Options(entityClass: Bar::class) | ||
)] | ||
class OrmBarResource | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public int $id; | ||
|
||
public string $name; | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/Fixtures/TestBundle/ApiResource/Issue6590/OrmFooResource.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,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Issue6590; | ||
|
||
use ApiPlatform\Doctrine\Orm\State\Options; | ||
use ApiPlatform\Metadata\ApiProperty; | ||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\GraphQl\Query; | ||
use ApiPlatform\Metadata\GraphQl\QueryCollection; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6590\Foo; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\Issue6590\FooResourceProvider; | ||
|
||
#[ApiResource( | ||
shortName: 'Issue6590OrmFoo', | ||
operations: [], | ||
graphQlOperations: [ | ||
new Query(), | ||
new QueryCollection(), | ||
], | ||
provider: FooResourceProvider::class, | ||
stateOptions: new Options(entityClass: Foo::class) | ||
)] | ||
class OrmFooResource | ||
{ | ||
#[ApiProperty(identifier: true)] | ||
public int $id; | ||
|
||
/** | ||
* @var OrmBarResource[] | ||
*/ | ||
public array $bars; | ||
|
||
public function addBar(OrmBarResource $bar): void | ||
{ | ||
$this->bars[] = $bar; | ||
} | ||
|
||
public function removeBar(OrmBarResource $bar): void | ||
{ | ||
unset($this->bars[array_search($bar, $this->bars, true)]); | ||
} | ||
} |
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 <[email protected]> | ||
* | ||
* 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\Issue6590; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity()] | ||
#[ORM\Table(name: 'bar6590')] | ||
class Bar | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private int $id; | ||
|
||
#[ORM\Column] | ||
private string $name; | ||
|
||
#[ORM\ManyToOne(targetEntity: Foo::class, inversedBy: 'bars')] | ||
private ?Foo $foo = null; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): self | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
public function getFoo(): ?Foo | ||
{ | ||
return $this->foo; | ||
} | ||
|
||
public function setFoo(?Foo $foo): self | ||
{ | ||
$this->foo = $foo; | ||
|
||
return $this; | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Issue6590; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
#[ORM\Table(name: 'foo6590')] | ||
class Foo | ||
{ | ||
#[ORM\Id] | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private int $id; | ||
|
||
#[ORM\OneToMany(targetEntity: Bar::class, mappedBy: 'foo')] | ||
private Collection $bars; | ||
|
||
public function __construct() | ||
{ | ||
$this->bars = new ArrayCollection(); | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* @return Collection<Bar> | ||
*/ | ||
public function getBars(): Collection | ||
{ | ||
return $this->bars; | ||
} | ||
|
||
/** | ||
* @param Collection<Bar> $bars | ||
*/ | ||
public function setBars(Collection $bars): self | ||
{ | ||
$this->bars = $bars; | ||
|
||
return $this; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
tests/Fixtures/TestBundle/State/Issue6590/BarResourceProvider.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,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Issue6590; | ||
|
||
use ApiPlatform\Doctrine\Orm\Paginator; | ||
use ApiPlatform\Metadata\CollectionOperationInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\Pagination\TraversablePaginator; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6590\OrmBarResource; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6590\Bar as BarEntity; | ||
|
||
class BarResourceProvider 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 CollectionOperationInterface) { | ||
$entities = $this->collectionProvider->provide($operation, $uriVariables, $context); | ||
\assert($entities instanceof Paginator); | ||
|
||
$resources = []; | ||
foreach ($entities as $entity) { | ||
$resources[] = $this->getResource($entity); | ||
} | ||
|
||
return new TraversablePaginator( | ||
new \ArrayIterator($resources), | ||
$entities->getCurrentPage(), | ||
$entities->getItemsPerPage(), | ||
$entities->getTotalItems() | ||
); | ||
} | ||
|
||
$entity = $this->itemProvider->provide($operation, $uriVariables, $context); | ||
|
||
return $this->getResource($entity); | ||
} | ||
|
||
protected function getResource(BarEntity $entity): OrmBarResource | ||
{ | ||
$resource = new OrmBarResource(); | ||
$resource->id = $entity->getId(); | ||
$resource->name = $entity->getName(); | ||
|
||
return $resource; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I understand the problem occurs only when it's an embed resource? I'm wondering though why this
entityClass
is wrong, are you able to trace that call and fix it in the class responsible from calling this? Basically at https://github.com/api-platform/core/blob/3.4/src/Doctrine/Orm/State/ItemProvider.php#L74 it should already be the correct class.