Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Apr 24, 2019
1 parent 9222ded commit fce9c71
Show file tree
Hide file tree
Showing 9 changed files with 526 additions and 114 deletions.
49 changes: 49 additions & 0 deletions features/bootstrap/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

declare(strict_types=1);

use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ExternalUser;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Address as AddressDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Answer as AnswerDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeItem as CompositeItemDocument;
Expand Down Expand Up @@ -81,6 +82,7 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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('john.doe@example.com');
$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('john.doe@example.com');
$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
*/
Expand Down
92 changes: 0 additions & 92 deletions features/graphql/non_resource.feature

This file was deleted.

152 changes: 152 additions & 0 deletions features/main/table_inheritance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,155 @@ Feature: Table inheritance
}
}
"""

@createSchema
Scenario: Create a table inherited resource
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/dummy_table_inheritance_children" with body:
"""
{"name": "foo", "nickname": "bar"}
"""
Then the response status code should be 201
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": {
"@type": {
"type": "string",
"pattern": "^DummyTableInheritanceChild$"
},
"@context": {
"type": "string",
"pattern": "^/contexts/DummyTableInheritanceChild$"
},
"@id": {
"type": "string",
"pattern": "^/dummy_table_inheritance_children/1$"
},
"name": {
"type": "string",
"pattern": "^foo$",
"required": "true"
},
"nickname": {
"type": "string",
"pattern": "^bar$",
"required": "true"
}
}
}
"""

@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": "^/users/\\d+$",
"required": "true"
}
}
},
"minItems": 3,
"maxItems": 3,
"required": "true"
}
}
}
"""

@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"
}
}
},
"minItems": 3,
"maxItems": 3,
"required": "true"
}
}
}
"""
16 changes: 16 additions & 0 deletions src/Bridge/Symfony/Routing/RouteNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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 !== $currentResourceClass && is_a($resourceClass, $currentResourceClass, true) && null !== $operation && (empty($methods) || \in_array('GET', $methods, true))) {
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));
}

Expand Down
Loading

0 comments on commit fce9c71

Please sign in to comment.