Skip to content

Commit 9ccf578

Browse files
authored
Support subresource resourceClass resolving (#3556)
1 parent beb03a6 commit 9ccf578

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* GraphQL: **BC** `operation` is now `operationName` to follow the standard (#3568)
1313
* OpenAPI: Add PHP default values to the documentation (#2386)
1414
* Deprecate using a validation groups generator service not implementing `ApiPlatform\Core\Bridge\Symfony\Validator\ValidationGroupsGeneratorInterface` (#3346)
15+
* Subresources: subresource resourceClass can now be defined as a container parameter in XML and Yaml definitions
1516

1617
## 2.5.6
1718

src/Metadata/Extractor/XmlExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private function getProperties(\SimpleXMLElement $resource): array
120120
'attributes' => $this->getAttributes($property, 'attribute'),
121121
'subresource' => $property->subresource ? [
122122
'collection' => $this->phpize($property->subresource, 'collection', 'bool'),
123-
'resourceClass' => $this->phpize($property->subresource, 'resourceClass', 'string'),
123+
'resourceClass' => $this->resolve($this->phpize($property->subresource, 'resourceClass', 'string')),
124124
'maxDepth' => $this->phpize($property->subresource, 'maxDepth', 'integer'),
125125
] : null,
126126
];

src/Metadata/Extractor/YamlExtractor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ private function extractProperties(array $resourceYaml, string $resourceName, st
100100
if (!\is_array($propertyValues)) {
101101
throw new InvalidArgumentException(sprintf('"%s" setting is expected to be null or an array, %s given in "%s".', $propertyName, \gettype($propertyValues), $path));
102102
}
103+
if (isset($propertyValues['subresource']['resourceClass'])) {
104+
$propertyValues['subresource']['resourceClass'] = $this->resolve($propertyValues['subresource']['resourceClass']);
105+
}
103106

104107
$this->resources[$resourceName]['properties'][$propertyName] = [
105108
'description' => $this->phpize($propertyValues, 'description', 'string'),

tests/Fixtures/FileConfigurations/resources_with_parameters.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="https://api-platform.com/schema/metadata
66
https://api-platform.com/schema/metadata/metadata-2.0.xsd">
7-
<resource class="%dummy_class%"/>
7+
<resource class="%dummy_class%">
8+
<property name="relatedOwnedDummy">
9+
<subresource resourceClass="%dummy_related_owned_class%" />
10+
</property>
11+
</resource>
812
<resource class="%dummy_class%Bis"/>
913
<resource
1014
class="%file_config_dummy_class%"

tests/Fixtures/FileConfigurations/resources_with_parameters.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
resources:
2-
'%dummy_class%': ~
2+
'%dummy_class%':
3+
properties:
4+
'relatedOwnedDummy':
5+
subresource: { resourceClass: '%dummy_related_owned_class%' }
36
'%dummy_class%Bis': ~
47
'%file_config_dummy_class%':
58
shortName: 'thedummyshortname'

tests/Metadata/Extractor/ExtractorTestCase.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ final public function testResourcesParametersResolution()
159159
{
160160
$containerProphecy = $this->prophesize(ContainerInterface::class);
161161
$containerProphecy->get('dummy_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy::class);
162+
$containerProphecy->getParameter('dummy_related_owned_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy::class);
162163
$containerProphecy->get('file_config_dummy_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy::class);
163164

164165
$resources = $this->createExtractor([$this->getResourceWithParametersFile()], $containerProphecy->reveal())->getResources();
@@ -173,7 +174,11 @@ final public function testResourcesParametersResolution()
173174
'subresourceOperations' => null,
174175
'graphql' => null,
175176
'attributes' => null,
176-
'properties' => null,
177+
'properties' => [
178+
'relatedOwnedDummy' => [
179+
'resourceClass' => \ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy::class,
180+
],
181+
],
177182
],
178183
'\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyBis' => [
179184
'shortName' => null,
@@ -285,6 +290,7 @@ final public function testResourcesParametersResolutionWithTheSymfonyContainer()
285290
{
286291
$containerProphecy = $this->prophesize(SymfonyContainerInterface::class);
287292
$containerProphecy->getParameter('dummy_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy::class);
293+
$containerProphecy->getParameter('dummy_related_owned_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy::class);
288294
$containerProphecy->getParameter('file_config_dummy_class')->willReturn(\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy::class);
289295

290296
$resources = $this->createExtractor([$this->getResourceWithParametersFile()], $containerProphecy->reveal())->getResources();
@@ -299,7 +305,11 @@ final public function testResourcesParametersResolutionWithTheSymfonyContainer()
299305
'subresourceOperations' => null,
300306
'graphql' => null,
301307
'attributes' => null,
302-
'properties' => null,
308+
'properties' => [
309+
'relatedOwnedDummy' => [
310+
'resourceClass' => \ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedOwnedDummy::class,
311+
],
312+
],
303313
],
304314
'\ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyBis' => [
305315
'shortName' => null,
@@ -421,7 +431,11 @@ final public function testResourcesParametersResolutionWithoutAContainer()
421431
'subresourceOperations' => null,
422432
'graphql' => null,
423433
'attributes' => null,
424-
'properties' => null,
434+
'properties' => [
435+
'relatedOwnedDummy' => [
436+
'resourceClass' => '%dummy_related_owned_class%',
437+
],
438+
],
425439
],
426440
'%dummy_class%Bis' => [
427441
'shortName' => null,

0 commit comments

Comments
 (0)