Skip to content

Commit

Permalink
Support subresource resourceClass resolving (#3556)
Browse files Browse the repository at this point in the history
  • Loading branch information
esserj authored May 30, 2020
1 parent beb03a6 commit 9ccf578
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* GraphQL: **BC** `operation` is now `operationName` to follow the standard (#3568)
* OpenAPI: Add PHP default values to the documentation (#2386)
* Deprecate using a validation groups generator service not implementing `ApiPlatform\Core\Bridge\Symfony\Validator\ValidationGroupsGeneratorInterface` (#3346)
* Subresources: subresource resourceClass can now be defined as a container parameter in XML and Yaml definitions

## 2.5.6

Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Extractor/XmlExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private function getProperties(\SimpleXMLElement $resource): array
'attributes' => $this->getAttributes($property, 'attribute'),
'subresource' => $property->subresource ? [
'collection' => $this->phpize($property->subresource, 'collection', 'bool'),
'resourceClass' => $this->phpize($property->subresource, 'resourceClass', 'string'),
'resourceClass' => $this->resolve($this->phpize($property->subresource, 'resourceClass', 'string')),
'maxDepth' => $this->phpize($property->subresource, 'maxDepth', 'integer'),
] : null,
];
Expand Down
3 changes: 3 additions & 0 deletions src/Metadata/Extractor/YamlExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ private function extractProperties(array $resourceYaml, string $resourceName, st
if (!\is_array($propertyValues)) {
throw new InvalidArgumentException(sprintf('"%s" setting is expected to be null or an array, %s given in "%s".', $propertyName, \gettype($propertyValues), $path));
}
if (isset($propertyValues['subresource']['resourceClass'])) {
$propertyValues['subresource']['resourceClass'] = $this->resolve($propertyValues['subresource']['resourceClass']);
}

$this->resources[$resourceName]['properties'][$propertyName] = [
'description' => $this->phpize($propertyValues, 'description', 'string'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
<resource class="%dummy_class%"/>
<resource class="%dummy_class%">
<property name="relatedOwnedDummy">
<subresource resourceClass="%dummy_related_owned_class%" />
</property>
</resource>
<resource class="%dummy_class%Bis"/>
<resource
class="%file_config_dummy_class%"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
resources:
'%dummy_class%': ~
'%dummy_class%':
properties:
'relatedOwnedDummy':
subresource: { resourceClass: '%dummy_related_owned_class%' }
'%dummy_class%Bis': ~
'%file_config_dummy_class%':
shortName: 'thedummyshortname'
Expand Down
20 changes: 17 additions & 3 deletions tests/Metadata/Extractor/ExtractorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ final public function testResourcesParametersResolution()
{
$containerProphecy = $this->prophesize(ContainerInterface::class);
$containerProphecy->get('dummy_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy::class);
$containerProphecy->getParameter('dummy_related_owned_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy::class);
$containerProphecy->get('file_config_dummy_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy::class);

$resources = $this->createExtractor([$this->getResourceWithParametersFile()], $containerProphecy->reveal())->getResources();
Expand All @@ -173,7 +174,11 @@ final public function testResourcesParametersResolution()
'subresourceOperations' => null,
'graphql' => null,
'attributes' => null,
'properties' => null,
'properties' => [
'relatedOwnedDummy' => [
'resourceClass' => \ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy::class,
],
],
],
'\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyBis' => [
'shortName' => null,
Expand Down Expand Up @@ -285,6 +290,7 @@ final public function testResourcesParametersResolutionWithTheSymfonyContainer()
{
$containerProphecy = $this->prophesize(SymfonyContainerInterface::class);
$containerProphecy->getParameter('dummy_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy::class);
$containerProphecy->getParameter('dummy_related_owned_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy::class);
$containerProphecy->getParameter('file_config_dummy_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy::class);

$resources = $this->createExtractor([$this->getResourceWithParametersFile()], $containerProphecy->reveal())->getResources();
Expand All @@ -299,7 +305,11 @@ final public function testResourcesParametersResolutionWithTheSymfonyContainer()
'subresourceOperations' => null,
'graphql' => null,
'attributes' => null,
'properties' => null,
'properties' => [
'relatedOwnedDummy' => [
'resourceClass' => \ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy::class,
],
],
],
'\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyBis' => [
'shortName' => null,
Expand Down Expand Up @@ -421,7 +431,11 @@ final public function testResourcesParametersResolutionWithoutAContainer()
'subresourceOperations' => null,
'graphql' => null,
'attributes' => null,
'properties' => null,
'properties' => [
'relatedOwnedDummy' => [
'resourceClass' => '%dummy_related_owned_class%',
],
],
],
'%dummy_class%Bis' => [
'shortName' => null,
Expand Down

0 comments on commit 9ccf578

Please sign in to comment.