From 9726c58b822cf4bb672a081855456595a625d201 Mon Sep 17 00:00:00 2001 From: soyuka Date: Wed, 13 Feb 2019 16:25:56 +0100 Subject: [PATCH] attempt of interface fix --- features/main/table_inheritance.feature | 66 +++++++++++---------- src/Api/ResourceClassResolver.php | 5 ++ src/Bridge/Symfony/Routing/IriConverter.php | 11 +++- src/JsonLd/Serializer/ItemNormalizer.php | 3 +- 4 files changed, 51 insertions(+), 34 deletions(-) diff --git a/features/main/table_inheritance.feature b/features/main/table_inheritance.feature index 230d877e693..54971c078aa 100644 --- a/features/main/table_inheritance.feature +++ b/features/main/table_inheritance.feature @@ -292,34 +292,38 @@ Feature: Table inheritance } """ - # Scenario: Get the parent interface collection - # When I send a "GET" request to "/resource_interfaces" - # Then 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 valid according to this schema: - # """ - # { - # "type": "object", - # "properties": { - # "hydra:member": { - # "type": "array", - # "items": { - # "type": "object", - # "properties": { - # "@type": { - # "type": "string", - # "pattern": "^ResourceInterface$" - # }, - # "foo": { - # "type": "string", - # "required": "true" - # } - # } - # }, - # "minItems": 1 - # } - # }, - # "required": ["hydra:member"] - # } - # """ + Scenario: Get the parent interface collection + When I send a "GET" request to "/resource_interfaces" + Then 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 valid according to this schema: + """ + { + "type": "object", + "properties": { + "hydra:member": { + "type": "array", + "items": { + "type": "object", + "properties": { + "@type": { + "type": "string", + "pattern": "^ResourceInterface$" + }, + "@id": { + "type": "string", + "pattern": "^_:" + }, + "foo": { + "type": "string", + "required": "true" + } + } + }, + "minItems": 1 + } + }, + "required": ["hydra:member"] + } + """ diff --git a/src/Api/ResourceClassResolver.php b/src/Api/ResourceClassResolver.php index 9f360c1de42..7d304da6380 100644 --- a/src/Api/ResourceClassResolver.php +++ b/src/Api/ResourceClassResolver.php @@ -54,6 +54,11 @@ public function getResourceClass($value, string $resourceClass = null, bool $str return $resourceClass; } + // The Resource is an interface + if ($value instanceof $resourceClass && $type !== $resourceClass && interface_exists($resourceClass)) { + return $resourceClass; + } + if ( ($isResourceClass ?? $this->isResourceClass($type)) || (is_subclass_of($type, $resourceClass) && $this->isResourceClass($resourceClass)) diff --git a/src/Bridge/Symfony/Routing/IriConverter.php b/src/Bridge/Symfony/Routing/IriConverter.php index dc5281caee6..61c6c538d9e 100644 --- a/src/Bridge/Symfony/Routing/IriConverter.php +++ b/src/Bridge/Symfony/Routing/IriConverter.php @@ -116,9 +116,14 @@ public function getItemFromIri(string $iri, array $context = []) /** * {@inheritdoc} */ - public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH): string + public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface::ABS_PATH/**, array $context = []**/): string { - $resourceClass = $this->getObjectClass($item); + if (3 === \count(\func_get_args())) { + $resourceClass = func_get_arg(2)['resource_class'] ?? $this->getObjectClass($item); + } else { + $resourceClass = $this->getObjectClass($item); + } + $routeName = $this->routeNameResolver->getRouteName($resourceClass, OperationType::ITEM); try { @@ -135,6 +140,8 @@ public function getIriFromItem($item, int $referenceType = UrlGeneratorInterface 'Unable to generate an IRI for the item of type "%s"', $resourceClass ), $e->getCode(), $e); + } catch (InvalidArgumentException $e) { + return '_:'.spl_object_id($item); } } diff --git a/src/JsonLd/Serializer/ItemNormalizer.php b/src/JsonLd/Serializer/ItemNormalizer.php index 18645707fe6..1f8ee8ac241 100644 --- a/src/JsonLd/Serializer/ItemNormalizer.php +++ b/src/JsonLd/Serializer/ItemNormalizer.php @@ -15,6 +15,7 @@ use ApiPlatform\Core\Api\IriConverterInterface; use ApiPlatform\Core\Api\ResourceClassResolverInterface; +use ApiPlatform\Core\Api\UrlGeneratorInterface; use ApiPlatform\Core\Exception\InvalidArgumentException; use ApiPlatform\Core\JsonLd\ContextBuilderInterface; use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; @@ -79,7 +80,7 @@ public function normalize($object, $format = null, array $context = []) // Use resolved resource class instead of given resource class to support multiple inheritance child types $context['resource_class'] = $resourceClass; - $context['iri'] = $this->iriConverter->getIriFromItem($object); + $context['iri'] = $this->iriConverter->getIriFromItem($object, UrlGeneratorInterface::ABS_PATH, $context); $rawData = parent::normalize($object, $format, $context); if (!\is_array($rawData)) {