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
48 changed files
with
776 additions
and
87 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,150 @@ | ||
Feature: Embedded resources support | ||
In order to use a hypermedia API | ||
As a client software developer | ||
I need to be able to retrieve embedded resources only | ||
|
||
@dropSchema | ||
Scenario: Just delete previous stuff pls | ||
|
||
@createSchema | ||
Scenario: Create a third level | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/third_levels" with body: | ||
""" | ||
{"level": 3} | ||
""" | ||
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 equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/ThirdLevel", | ||
"@id": "/third_levels/1", | ||
"@type": "ThirdLevel", | ||
"id": 1, | ||
"level": 3, | ||
"test": true | ||
} | ||
""" | ||
|
||
Scenario: Create a related dummy | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/related_dummies" with body: | ||
""" | ||
{"thirdLevel": "/third_levels/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 JSON should be equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/RelatedDummy", | ||
"@id": "/related_dummies/1", | ||
"@type": "https://schema.org/Product", | ||
"name": null, | ||
"dummyDate": null, | ||
"thirdLevel": "/third_levels/1", | ||
"relatedToDummyFriend": null, | ||
"dummyBoolean": null, | ||
"id": 1, | ||
"symfony": "symfony", | ||
"age": null | ||
} | ||
""" | ||
|
||
Scenario: Create a dummy with relations | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "POST" request to "/dummies" with body: | ||
""" | ||
{ | ||
"name": "Dummy with relations", | ||
"relatedDummy": "http://example.com/related_dummies/1", | ||
"relatedDummies": [ | ||
"/related_dummies/1" | ||
], | ||
"name_converted": null | ||
} | ||
""" | ||
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 equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/Dummy", | ||
"@id": "/dummies/1", | ||
"@type": "Dummy", | ||
"description": null, | ||
"dummy": null, | ||
"dummyBoolean": null, | ||
"dummyDate": null, | ||
"dummyFloat": null, | ||
"dummyPrice": null, | ||
"relatedDummy": "/related_dummies/1", | ||
"relatedDummies": [ | ||
"/related_dummies/1" | ||
], | ||
"jsonData": [], | ||
"name_converted": null, | ||
"id": 1, | ||
"name": "Dummy with relations", | ||
"alias": null | ||
} | ||
""" | ||
|
||
Scenario: Get the embedded relation collection | ||
When I send a "GET" request to "/dummies/1/related_dummies" | ||
And 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/RelatedDummy", | ||
"@id": "/related_dummies", | ||
"@type": "hydra:Collection", | ||
"hydra:member": [ | ||
{ | ||
"@id": "/related_dummies/1", | ||
"@type": "https://schema.org/Product", | ||
"name": null, | ||
"dummyDate": null, | ||
"thirdLevel": "/third_levels/1", | ||
"relatedToDummyFriend": [], | ||
"dummyBoolean": null, | ||
"id": 1, | ||
"symfony": "symfony", | ||
"age": null | ||
} | ||
], | ||
"hydra:totalItems": 1 | ||
} | ||
""" | ||
|
||
@dropSchema | ||
Scenario: Get the embedded relation collection | ||
When I send a "GET" request to "/dummies/1/related_dummies/1/third_levels" | ||
And 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/ThirdLevel", | ||
"@id": "/third_levels", | ||
"@type": "hydra:Collection", | ||
"hydra:member": [ | ||
{ | ||
"@id": "/third_levels/1", | ||
"@type": "ThirdLevel", | ||
"id": 1, | ||
"level": 3, | ||
"test": true | ||
} | ||
], | ||
"hydra:totalItems": 1 | ||
} | ||
""" | ||
|
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 |
---|---|---|
|
@@ -65,4 +65,9 @@ final class ApiProperty | |
* @var array | ||
*/ | ||
public $attributes = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $subcollection; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?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. | ||
*/ | ||
|
||
namespace ApiPlatform\Core\Bridge\Doctrine\Orm; | ||
|
||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\IdentifierManagerTrait; | ||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator; | ||
use ApiPlatform\Core\DataProvider\SubcollectionDataProviderInterface; | ||
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; | ||
use ApiPlatform\Core\Exception\RuntimeException; | ||
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; | ||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\ORM\Mapping\ClassMetadataInfo; | ||
|
||
/** | ||
* Subcollection data provider for the Doctrine ORM. | ||
* | ||
* @author Antoine Bluchet <soyuka@gmail.com> | ||
*/ | ||
class SubcollectionDataProvider implements SubcollectionDataProviderInterface | ||
{ | ||
use IdentifierManagerTrait; | ||
|
||
private $managerRegistry; | ||
private $collectionExtensions; | ||
|
||
/** | ||
* @param ManagerRegistry $managerRegistry | ||
* @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory | ||
* @param PropertyMetadataFactoryInterface $propertyMetadataFactory | ||
* @param QueryItemExtensionInterface[] $itemExtensions | ||
*/ | ||
public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $collectionExtensions = []) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory; | ||
$this->propertyMetadataFactory = $propertyMetadataFactory; | ||
$this->collectionExtensions = $collectionExtensions; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
public function getSubcollection(string $resourceClass, array $identifiers, array $context, string $operationName) | ||
{ | ||
$manager = $this->managerRegistry->getManagerForClass($resourceClass); | ||
if (null === $manager) { | ||
throw new ResourceClassNotSupportedException(); | ||
} | ||
|
||
$repository = $manager->getRepository($resourceClass); | ||
if (!method_exists($repository, 'createQueryBuilder')) { | ||
throw new RuntimeException('The repository class must have a "createQueryBuilder" method.'); | ||
} | ||
|
||
$queryBuilder = $repository->createQueryBuilder('o'); | ||
$queryNameGenerator = new QueryNameGenerator(); | ||
$previousQueryBuilder = null; | ||
$previousAlias = null; | ||
|
||
$num = count($context['identifiers']); | ||
|
||
while ($num--) { | ||
list($identifier, $identifierResourceClass) = $context['identifiers'][$num]; | ||
$previousIdentifier = $context['identifiers'][$num + 1][0] ?? $context['property']; | ||
|
||
$classMetadata = $manager->getClassMetadata($identifierResourceClass); | ||
|
||
$qb = $manager->createQueryBuilder(); | ||
$alias = $queryNameGenerator->generateJoinAlias($identifier); | ||
|
||
if (null !== $previousIdentifier) { | ||
//MANY_TO_MANY relations needs an explicit join so that the identifier part can be retrieved | ||
if ($classMetadata->getAssociationMapping($previousIdentifier)['type'] === ClassMetadataInfo::MANY_TO_MANY) { | ||
$joinAlias = $queryNameGenerator->generateJoinAlias($previousIdentifier); | ||
|
||
$qb->select($joinAlias) | ||
->from($identifierResourceClass, $alias) | ||
->innerJoin("$alias.$previousIdentifier", $joinAlias); | ||
} else { | ||
$qb->select("IDENTITY($alias.$previousIdentifier)") | ||
->from($identifierResourceClass, $alias); | ||
} | ||
} | ||
|
||
$normalizedIdentifiers = $this->normalizeIdentifiers($identifiers[$identifier], $manager, $identifierResourceClass); | ||
|
||
foreach ($normalizedIdentifiers as $key => $value) { | ||
$placeholder = $queryNameGenerator->generateParameterName($key); | ||
$qb->andWhere("$alias.$key = :$placeholder"); | ||
$queryBuilder->setParameter($placeholder, $value); | ||
} | ||
|
||
if (null === $previousQueryBuilder) { | ||
$previousQueryBuilder = $qb; | ||
} else { | ||
$previousQueryBuilder->andWhere($qb->expr()->in($previousAlias, $qb->getDQL())); | ||
} | ||
|
||
$previousAlias = $alias; | ||
} | ||
|
||
$queryBuilder->where( | ||
$queryBuilder->expr()->in('o', $previousQueryBuilder->getDQL()) | ||
); | ||
|
||
foreach ($this->collectionExtensions as $extension) { | ||
$extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName); | ||
|
||
if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) { | ||
return $extension->getResult($queryBuilder); | ||
} | ||
} | ||
|
||
return $queryBuilder->getQuery()->getResult(); | ||
} | ||
} |
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.