Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions features/bootstrap/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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('[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
*/
Expand Down
113 changes: 113 additions & 0 deletions features/main/table_inheritance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,116 @@ Feature: Table inheritance
}
}
"""

@!mongodb
@createSchema
@dropSchema
Copy link
Contributor

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

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"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
},
"additionalProperties": false

},
"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"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
},
"additionalProperties": false

},
"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 !== $operation && (empty($methods) || \in_array('GET', $methods, true)) && null !== $currentResourceClass && is_a($resourceClass, $currentResourceClass, true)) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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));
}

Expand Down
Loading