Skip to content

Commit 5ef88f8

Browse files
authored
Merge pull request #2086 from Nek-/feature/ignore-property-exist-test-for-interfaces
Ignore property that does not exist for interfaces (identifiers for interfaces)
2 parents 9eafd71 + e05809e commit 5ef88f8

File tree

8 files changed

+72
-11
lines changed

8 files changed

+72
-11
lines changed

features/main/table_inheritance.feature

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,11 @@ Feature: Table inheritance
307307
"items": {
308308
"type": "object",
309309
"properties": {
310-
"@type": {
311-
"type": "string",
312-
"pattern": "^ResourceInterface$"
313-
},
314-
"@id": {
310+
"foo": {
315311
"type": "string",
316-
"pattern": "^_:"
312+
"required": "true"
317313
},
318-
"foo": {
314+
"fooz": {
319315
"type": "string",
320316
"required": "true"
321317
}
@@ -327,3 +323,30 @@ Feature: Table inheritance
327323
"required": ["hydra:member"]
328324
}
329325
"""
326+
327+
Scenario: Get an interface resource item
328+
When I send a "GET" request to "/resource_interfaces/some-id"
329+
Then the response status code should be 200
330+
And the response should be in JSON
331+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
332+
And the JSON should be valid according to this schema:
333+
"""
334+
{
335+
"type": "object",
336+
"properties": {
337+
"context": {
338+
"type": "string",
339+
"pattern": "ResourceInterface$"
340+
},
341+
"foo": {
342+
"type": "string",
343+
"required": "true"
344+
},
345+
"fooz": {
346+
"type": "string",
347+
"required": "true",
348+
"pattern": "fooz"
349+
}
350+
}
351+
}
352+
"""

src/Metadata/Property/Factory/ExtractorPropertyMetadataFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ public function create(string $resourceClass, string $property, array $options =
4848
}
4949
}
5050

51+
$isInterface = interface_exists($resourceClass);
52+
5153
if (
52-
!property_exists($resourceClass, $property) ||
53-
!$propertyMetadata = $this->extractor->getResources()[$resourceClass]['properties'][$property] ?? false
54+
!property_exists($resourceClass, $property) && !$isInterface ||
55+
null === ($propertyMetadata = $this->extractor->getResources()[$resourceClass]['properties'][$property] ?? null)
5456
) {
5557
return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
5658
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
resources:
2-
'ApiPlatform\Core\Tests\Fixtures\DummyResourceInterface': ~
2+
'ApiPlatform\Core\Tests\Fixtures\DummyResourceInterface':
3+
properties:
4+
something:
5+
identifier: true
6+
somethingElse:
7+
writable: false
8+
readable: true

tests/Fixtures/TestBundle/DataProvider/ResourceInterfaceImplementationDataProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public function supports(string $resourceClass, string $operationName = null, ar
2828

2929
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
3030
{
31-
return (new ResourceInterfaceImplementation())->setFoo('single item');
31+
if ('some-id' === $id) {
32+
return (new ResourceInterfaceImplementation())->setFoo('single item');
33+
}
34+
35+
return null;
3236
}
3337

3438
public function getCollection(string $resourceClass, string $operationName = null)

tests/Fixtures/TestBundle/OtherResources/ResourceInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
interface ResourceInterface
1717
{
1818
public function getFoo(): string;
19+
20+
public function getFooz(): string;
1921
}

tests/Fixtures/TestBundle/OtherResources/ResourceInterfaceImplementation.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ public function getBar(): ?string
5151
{
5252
return $this->bar;
5353
}
54+
55+
public function getFooz(): string
56+
{
57+
return 'fooz';
58+
}
5459
}

tests/Fixtures/TestBundle/Resources/config/api_resources.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ resources:
77
collectionOperations:
88
get:
99
method: 'GET'
10+
11+
properties:
12+
foo:
13+
identifier: true

tests/Metadata/Property/Factory/ExtractorPropertyMetadataFactoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2222
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
2323
use ApiPlatform\Core\Metadata\Property\SubresourceMetadata;
24+
use ApiPlatform\Core\Tests\Fixtures\DummyResourceInterface;
2425
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy;
2526
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
2627
use Doctrine\Common\Collections\ArrayCollection;
@@ -237,4 +238,18 @@ public function testCreateWithMalformedYaml()
237238

238239
(new ExtractorPropertyMetadataFactory(new YamlExtractor([$configPath])))->create(FileConfigDummy::class, 'foo');
239240
}
241+
242+
public function testItExtractPropertiesFromInterfaceResources()
243+
{
244+
$configPath = __DIR__.'/../../../Fixtures/FileConfigurations/interface_resource.yml';
245+
246+
$propertyMetadataFactory = new ExtractorPropertyMetadataFactory(new YamlExtractor([$configPath]));
247+
$metadataSomething = $propertyMetadataFactory->create(DummyResourceInterface::class, 'something');
248+
$metadataSomethingElse = $propertyMetadataFactory->create(DummyResourceInterface::class, 'somethingElse');
249+
250+
$this->assertInstanceOf(PropertyMetadata::class, $metadataSomething);
251+
$this->assertInstanceOf(PropertyMetadata::class, $metadataSomethingElse);
252+
$this->assertTrue($metadataSomething->isIdentifier());
253+
$this->assertFalse($metadataSomethingElse->isWritable());
254+
}
240255
}

0 commit comments

Comments
 (0)