Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
636 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
Feature: Uri Variables | ||
|
||
@createSchema | ||
@php8 | ||
Scenario: Create a resource Company | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/companies" with body: | ||
""" | ||
{ | ||
"name": "Foo Company 1" | ||
} | ||
""" | ||
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 header "Content-Location" should be equal to "/companies/1" | ||
And the header "Location" should be equal to "/companies/1" | ||
And the JSON should be equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/Company", | ||
"@id": "/companies/1", | ||
"@type": "Company", | ||
"id": 1, | ||
"name": "Foo Company 1", | ||
"employees": null | ||
} | ||
""" | ||
|
||
@php8 | ||
Scenario: Create a second resource Company | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/companies" with body: | ||
""" | ||
{ | ||
"name": "Foo Company 2" | ||
} | ||
""" | ||
Then the response status code should be 201 | ||
|
||
@php8 | ||
Scenario: Create first Employee | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/employees" with body: | ||
""" | ||
{ | ||
"name": "foo", | ||
"company": "/companies/1" | ||
} | ||
""" | ||
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 header "Content-Location" should be equal to "/companies/1/employees/1" | ||
And the header "Location" should be equal to "/companies/1/employees/1" | ||
And the JSON should be equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/Employee", | ||
"@id": "/companies/1/employees/1", | ||
"@type": "Employee", | ||
"id": 1, | ||
"name": "foo", | ||
"company": "/companies/1" | ||
} | ||
""" | ||
|
||
@php8 | ||
Scenario: Create second Employee | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/employees" with body: | ||
""" | ||
{ | ||
"name": "foo2", | ||
"company": "/companies/2" | ||
} | ||
""" | ||
Then the response status code should be 201 | ||
|
||
@php8 | ||
Scenario: Create thirf Employee | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/employees" with body: | ||
""" | ||
{ | ||
"name": "foo3", | ||
"company": "/companies/2" | ||
} | ||
""" | ||
Then the response status code should be 201 | ||
|
||
@php8 | ||
Scenario: Create a resource Company | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/companies" with body: | ||
""" | ||
{ | ||
"name": "bar", | ||
"company": "/companies/1" | ||
} | ||
""" | ||
Then the response status code should be 201 | ||
|
||
@php8 | ||
Scenario: Create a second resource Company | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/companies" with body: | ||
""" | ||
{ | ||
"name": "toto", | ||
"company": "/companies/2" | ||
} | ||
""" | ||
Then the response status code should be 201 | ||
|
||
@php8 | ||
Scenario: Retrieve the collection of employees | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "GET" request to "/companies/2/employees" | ||
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 equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/Employee", | ||
"@id": "/companies/2/employees", | ||
"@type": "hydra:Collection", | ||
"hydra:member": [ | ||
{ | ||
"@id": "/companies/2/employees/2", | ||
"@type": "Employee", | ||
"id": 2, | ||
"name": "foo2", | ||
"company": "/companies/2" | ||
}, | ||
{ | ||
"@id": "/companies/2/employees/3", | ||
"@type": "Employee", | ||
"id": 3, | ||
"name": "foo3", | ||
"company": "/companies/2" | ||
} | ||
], | ||
"hydra:totalItems": 2 | ||
} | ||
""" | ||
|
||
@php8 | ||
Scenario: Retrieve the company of an employee | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "GET" request to "/employees/1/company" | ||
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 equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/Company", | ||
"@id": "/companies/1", | ||
"@type": "Company", | ||
"id": 1, | ||
"name": "Foo Company 1", | ||
"employees": null | ||
} | ||
""" | ||
|
||
@php8 | ||
Scenario: Retrieve an employee | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "GET" request to "/companies/1/employees/1" | ||
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 equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/Employee", | ||
"@id": "/companies/1/employees/1", | ||
"@type": "Employee", | ||
"id": 1, | ||
"name": "foo", | ||
"company": "/companies/1" | ||
} | ||
""" | ||
|
||
@php8 | ||
Scenario: Trying to get an employee of wrong company | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "GET" request to "/companies/1/employees/2" | ||
Then the response status code should be 404 |
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
73 changes: 73 additions & 0 deletions
73
src/Bridge/Doctrine/Orm/State/UriVariablesHandlerTrait.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,73 @@ | ||
<?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\Bridge\Doctrine\Orm\State; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
trait UriVariablesHandlerTrait | ||
{ | ||
private function handleUriVariables(QueryBuilder $queryBuilder, array $identifiers, QueryNameGenerator $queryNameGenerator, array $context, string $resourceClass, ?string $operationName = null): void | ||
{ | ||
$operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($operationName); | ||
$uriVariables = $operation->getUriVariables(); | ||
|
||
if (null === $uriVariables) { | ||
return; | ||
} | ||
$alias = $queryBuilder->getRootAliases()[0]; | ||
|
||
foreach ($identifiers as $identifier => $value) { | ||
$uriVariable = $uriVariables[$identifier] ?? $uriVariables['id']; | ||
|
||
$placeholder = ':id_'.$identifier; | ||
if ($inverseProperty = $uriVariable->getInverseProperty()) { | ||
$propertyIdentifier = $uriVariable->getIdentifiers()[0]; | ||
$joinAlias = $queryNameGenerator->generateJoinAlias($inverseProperty); | ||
|
||
$queryBuilder->join( | ||
$uriVariable->getTargetClass(), | ||
$joinAlias, | ||
'with', | ||
"$alias.$propertyIdentifier = $joinAlias.$inverseProperty" | ||
); | ||
|
||
$expression = $queryBuilder->expr()->eq( | ||
"{$joinAlias}.{$propertyIdentifier}", | ||
$placeholder | ||
); | ||
} elseif ($property = $uriVariable->getProperty()) { | ||
$propertyIdentifier = $uriVariable->getIdentifiers()[0]; | ||
$joinAlias = $queryNameGenerator->generateJoinAlias($property); | ||
|
||
$queryBuilder->join( | ||
"$alias.$property", | ||
$joinAlias, | ||
); | ||
|
||
$expression = $queryBuilder->expr()->eq( | ||
"{$joinAlias}.{$propertyIdentifier}", | ||
$placeholder | ||
); | ||
} else { | ||
$propertyIdentifier = $uriVariable->getIdentifiers()[0]; | ||
$expression = $queryBuilder->expr()->eq( | ||
"{$alias}.{$propertyIdentifier}", $placeholder | ||
); | ||
} | ||
$queryBuilder->andWhere($expression); | ||
$queryBuilder->setParameter($placeholder, $value); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.