-
-
Notifications
You must be signed in to change notification settings - Fork 940
Get resource from parent #2760
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
Get resource from parent #2760
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,11 +76,13 @@ | |
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyProperty; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\EmbeddableDummy; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\EmbeddedDummy; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ExternalUser; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Foo; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FooDummy; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FourthLevel; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Greeting; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\InternalUser; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Node; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Order; | ||
|
|
@@ -95,6 +97,7 @@ | |
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedToDummyFriend; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelationEmbedder; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SecuredDummy; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Site; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ThirdLevel; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\User; | ||
| use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UuidIdentifierDummy; | ||
|
|
@@ -165,6 +168,52 @@ public function thereAreDummyObjects(int $nb) | |
| $this->manager->flush(); | ||
| } | ||
|
|
||
| /** | ||
| * @Given there are :nb sites with internal owner | ||
| */ | ||
| public function thereAreSitesWithInternalOwner(int $nb) | ||
| { | ||
| for ($i = 1; $i <= $nb; ++$i) { | ||
| $internalUser = new InternalUser(); | ||
| $internalUser->setFirstname('Internal'); | ||
| $internalUser->setLastname('User'); | ||
| $internalUser->setEmail('[email protected]'); | ||
| $internalUser->setInternalId('INT'); | ||
|
|
||
| $site = new Site(); | ||
| $site->setTitle('title'); | ||
| $site->setDescription('description'); | ||
| $site->setOwner($internalUser); | ||
|
|
||
| $this->manager->persist($site); | ||
| } | ||
|
|
||
| $this->manager->flush(); | ||
| } | ||
|
|
||
| /** | ||
| * @Given there are :nb sites with external owner | ||
| */ | ||
| public function thereAreSitesWithExternalOwner(int $nb) | ||
| { | ||
| for ($i = 1; $i <= $nb; ++$i) { | ||
| $externalUser = new ExternalUser(); | ||
| $externalUser->setFirstname('External'); | ||
| $externalUser->setLastname('User'); | ||
| $externalUser->setEmail('[email protected]'); | ||
| $externalUser->setExternalId('EXT'); | ||
|
|
||
| $site = new Site(); | ||
| $site->setTitle('title'); | ||
| $site->setDescription('description'); | ||
| $site->setOwner($externalUser); | ||
|
|
||
| $this->manager->persist($site); | ||
| } | ||
|
|
||
| $this->manager->flush(); | ||
| } | ||
|
|
||
| /** | ||
| * @Given there are :nb foo objects with fake names | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -350,3 +350,116 @@ Feature: Table inheritance | |||||||
| } | ||||||||
| } | ||||||||
| """ | ||||||||
|
|
||||||||
| @!mongodb | ||||||||
| @createSchema | ||||||||
| @dropSchema | ||||||||
| Scenario: Generate iri from parent resource | ||||||||
| Given there are 3 sites with internal owner | ||||||||
| When I add "Content-Type" header equal to "application/ld+json" | ||||||||
| And I send a "GET" request to "/sites" | ||||||||
| Then the response status code should be 200 | ||||||||
| And the response should be in JSON | ||||||||
| And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" | ||||||||
| And the JSON should be valid according to this schema: | ||||||||
| """ | ||||||||
| { | ||||||||
| "type": "object", | ||||||||
| "properties": { | ||||||||
| "hydra:member": { | ||||||||
| "type": "array", | ||||||||
| "items": { | ||||||||
| "type": "object", | ||||||||
| "properties": { | ||||||||
| "@type": { | ||||||||
| "type": "string", | ||||||||
| "pattern": "^Site$", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "@id": { | ||||||||
| "type": "string", | ||||||||
| "pattern": "^/sites/\\d+$", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "id": { | ||||||||
| "type": "integer", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "title": { | ||||||||
| "type": "string", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "description": { | ||||||||
| "type": "string", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "owner": { | ||||||||
| "type": "string", | ||||||||
| "pattern": "^/custom_users/\\d+$", | ||||||||
| "required": "true" | ||||||||
| } | ||||||||
| } | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| }, | ||||||||
| "minItems": 3, | ||||||||
| "maxItems": 3, | ||||||||
| "required": "true" | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| """ | ||||||||
|
|
||||||||
| @!mongodb | ||||||||
| @createSchema | ||||||||
| Scenario: Generate iri from current resource even if parent class is a resource | ||||||||
| Given there are 3 sites with external owner | ||||||||
| When I add "Content-Type" header equal to "application/ld+json" | ||||||||
| And I send a "GET" request to "/sites" | ||||||||
| Then the response status code should be 200 | ||||||||
| And the response should be in JSON | ||||||||
| And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" | ||||||||
| And the JSON should be valid according to this schema: | ||||||||
| """ | ||||||||
| { | ||||||||
| "type": "object", | ||||||||
| "properties": { | ||||||||
| "hydra:member": { | ||||||||
| "type": "array", | ||||||||
| "items": { | ||||||||
| "type": "object", | ||||||||
| "properties": { | ||||||||
| "@type": { | ||||||||
| "type": "string", | ||||||||
| "pattern": "^Site$", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "@id": { | ||||||||
| "type": "string", | ||||||||
| "pattern": "^/sites/\\d+$", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "id": { | ||||||||
| "type": "integer", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "title": { | ||||||||
| "type": "string", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "description": { | ||||||||
| "type": "string", | ||||||||
| "required": "true" | ||||||||
| }, | ||||||||
| "owner": { | ||||||||
| "type": "string", | ||||||||
| "pattern": "^/external_users/\\d+$", | ||||||||
| "required": "true" | ||||||||
| } | ||||||||
| } | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| }, | ||||||||
| "minItems": 3, | ||||||||
| "maxItems": 3, | ||||||||
| "required": "true" | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| """ | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ public function getRouteName(string $resourceClass, $operationType /*, array $co | |
|
|
||
| $operationType = OperationTypeDeprecationHelper::getOperationType($operationType); | ||
|
|
||
| // Try with strict class name | ||
| foreach ($this->router->getRouteCollection()->all() as $routeName => $route) { | ||
| $currentResourceClass = $route->getDefault('_api_resource_class'); | ||
| $operation = $route->getDefault(sprintf('_api_%s_operation_name', $operationType)); | ||
|
|
@@ -59,6 +60,21 @@ public function getRouteName(string $resourceClass, $operationType /*, array $co | |
| } | ||
| } | ||
|
|
||
| // Maybe the parent class is a resource | ||
| foreach ($this->router->getRouteCollection()->all() as $routeName => $route) { | ||
| $currentResourceClass = $route->getDefault('_api_resource_class'); | ||
| $operation = $route->getDefault(sprintf('_api_%s_operation_name', $operationType)); | ||
| $methods = $route->getMethods(); | ||
|
|
||
| if (null !== $operation && (empty($methods) || \in_array('GET', $methods, true)) && null !== $currentResourceClass && is_a($resourceClass, $currentResourceClass, true)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just asking but maybe we could add this if statement in the previous loop and saved the first value found that would be returned at the after the loop if not null to avoid looping through all routes twice here ? |
||
| if (OperationType::SUBRESOURCE === $operationType && false === $this->isSameSubresource($context, $route->getDefault('_api_subresource_context'))) { | ||
| continue; | ||
| } | ||
|
|
||
| return $routeName; | ||
| } | ||
| } | ||
|
|
||
| throw new InvalidArgumentException(sprintf('No %s route associated with the type "%s".', $operationType, $resourceClass)); | ||
| } | ||
|
|
||
|
|
||
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.
This doesn't exists anymore IIRC