Skip to content

Commit

Permalink
attempt of interface fix
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Feb 13, 2019
1 parent 573d489 commit 9726c58
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
66 changes: 35 additions & 31 deletions features/main/table_inheritance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
"""
5 changes: 5 additions & 0 deletions src/Api/ResourceClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 9 additions & 2 deletions src/Bridge/Symfony/Routing/IriConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 9726c58

Please sign in to comment.