Skip to content

Commit 9726c58

Browse files
committed
attempt of interface fix
1 parent 573d489 commit 9726c58

File tree

4 files changed

+51
-34
lines changed

4 files changed

+51
-34
lines changed

features/main/table_inheritance.feature

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -292,34 +292,38 @@ Feature: Table inheritance
292292
}
293293
"""
294294

295-
# Scenario: Get the parent interface collection
296-
# When I send a "GET" request to "/resource_interfaces"
297-
# Then the response status code should be 200
298-
# And the response should be in JSON
299-
# And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
300-
# And the JSON should be valid according to this schema:
301-
# """
302-
# {
303-
# "type": "object",
304-
# "properties": {
305-
# "hydra:member": {
306-
# "type": "array",
307-
# "items": {
308-
# "type": "object",
309-
# "properties": {
310-
# "@type": {
311-
# "type": "string",
312-
# "pattern": "^ResourceInterface$"
313-
# },
314-
# "foo": {
315-
# "type": "string",
316-
# "required": "true"
317-
# }
318-
# }
319-
# },
320-
# "minItems": 1
321-
# }
322-
# },
323-
# "required": ["hydra:member"]
324-
# }
325-
# """
295+
Scenario: Get the parent interface collection
296+
When I send a "GET" request to "/resource_interfaces"
297+
Then the response status code should be 200
298+
And the response should be in JSON
299+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
300+
And the JSON should be valid according to this schema:
301+
"""
302+
{
303+
"type": "object",
304+
"properties": {
305+
"hydra:member": {
306+
"type": "array",
307+
"items": {
308+
"type": "object",
309+
"properties": {
310+
"@type": {
311+
"type": "string",
312+
"pattern": "^ResourceInterface$"
313+
},
314+
"@id": {
315+
"type": "string",
316+
"pattern": "^_:"
317+
},
318+
"foo": {
319+
"type": "string",
320+
"required": "true"
321+
}
322+
}
323+
},
324+
"minItems": 1
325+
}
326+
},
327+
"required": ["hydra:member"]
328+
}
329+
"""

src/Api/ResourceClassResolver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function getResourceClass($value, string $resourceClass = null, bool $str
5454
return $resourceClass;
5555
}
5656

57+
// The Resource is an interface
58+
if ($value instanceof $resourceClass && $type !== $resourceClass && interface_exists($resourceClass)) {
59+
return $resourceClass;
60+
}
61+
5762
if (
5863
($isResourceClass ?? $this->isResourceClass($type))
5964
|| (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass))

src/Bridge/Symfony/Routing/IriConverter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,14 @@ public function getItemFromIri(string $iri, array $context = [])
116116
/**
117117
* {@inheritdoc}
118118
*/
119-
public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string
119+
public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH/**, array $context = []**/): string
120120
{
121-
$resourceClass = $this->getObjectClass($item);
121+
if (3 === \count(\func_get_args())) {
122+
$resourceClass = func_get_arg(2)['resource_class'] ?? $this->getObjectClass($item);
123+
} else {
124+
$resourceClass = $this->getObjectClass($item);
125+
}
126+
122127
$routeName = $this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM);
123128

124129
try {
@@ -135,6 +140,8 @@ public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface
135140
'Unable to generate an IRI for the item of type "%s"',
136141
$resourceClass
137142
), $e->getCode(), $e);
143+
} catch (InvalidArgumentException $e) {
144+
return '_:'.spl_object_id($item);
138145
}
139146
}
140147

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
18+
use ApiPlatform\Core\Api\UrlGeneratorInterface;
1819
use ApiPlatform\Core\Exception\InvalidArgumentException;
1920
use ApiPlatform\Core\JsonLd\ContextBuilderInterface;
2021
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
@@ -79,7 +80,7 @@ public function normalize($object, $format = null, array $context = [])
7980

8081
// Use resolved resource class instead of given resource class to support multiple inheritance child types
8182
$context['resource_class'] = $resourceClass;
82-
$context['iri'] = $this->iriConverter->getIriFromItem($object);
83+
$context['iri'] = $this->iriConverter->getIriFromItem($object, UrlGeneratorInterface::ABS_PATH, $context);
8384

8485
$rawData = parent::normalize($object, $format, $context);
8586
if (!\is_array($rawData)) {

0 commit comments

Comments
 (0)