-
-
Notifications
You must be signed in to change notification settings - Fork 844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds jsonld context, Adds support for jsonld typed values #752
Conversation
@@ -107,6 +107,11 @@ public function getResourceContext(string $resourceClass, int $referenceType = U | |||
'@id' => $id, | |||
'@type' => '@id', | |||
]; | |||
} else if (null !== $propertyMetadata->getValueType()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be simplified using elseif
.
This method should be updated too: https://github.com/api-platform/core/blob/master/src/Hydra/Serializer/DocumentationNormalizer.php#L282-L319 |
What about having these as attributes, so that it can be JSON-LD specific, instead of trying to find a general enough "type" declaration that fits all the formats (sounds like an impossible task). |
I can also move it to the attributes. What would be the convention for the name? |
There is already a |
So it would be something like ? /**
* @var string | array
*
* @ApiProperty(iri="http://xmlns.com/foaf/0.1/lastName", attributes={"jsonld_context": {"value_type":"literal"}})
*
*/
private $lastName; |
Tyes, it looks better! |
/**
* @var string
*
* @ApiProperty(iri="http://xmlns.com/foaf/0.1/lastName", attributes={
* "jsonld_context"={"@type"="literal"},
* })
*
*/
private $lastName; |
Looks fine for me, I guess that's the way to go:) |
@@ -97,6 +97,7 @@ public function getResourceContext(string $resourceClass, int $referenceType = U | |||
} | |||
|
|||
$convertedName = $this->nameConverter ? $this->nameConverter->normalize($propertyName) : $propertyName; | |||
$jsonldContext = $propertyMetadata->getAttributes()['jsonld_context'] ?? []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's set in the jsonld_context
attribute should be used as the base. We don't want to limit it to just @type
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See for example how hydra_context
is handled in
core/src/Hydra/Serializer/DocumentationNormalizer.php
Lines 209 to 273 in 7ac1866
/** | |
* Gets and populates if applicable a Hydra operation. | |
* | |
* @param string $resourceClass | |
* @param ResourceMetadata $resourceMetadata | |
* @param string $operationName | |
* @param array $operation | |
* @param string $prefixedShortName | |
* @param bool $collection | |
* | |
* @return array | |
*/ | |
private function getHydraOperation(string $resourceClass, ResourceMetadata $resourceMetadata, string $operationName, array $operation, string $prefixedShortName, bool $collection) : array | |
{ | |
if ($collection) { | |
$method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $operationName); | |
} else { | |
$method = $this->operationMethodResolver->getItemOperationMethod($resourceClass, $operationName); | |
} | |
$hydraOperation = $operation['hydra_context'] ?? []; | |
$shortName = $resourceMetadata->getShortName(); | |
if ('GET' === $method && $collection) { | |
$hydraOperation = [ | |
'hydra:title' => sprintf('Retrieves the collection of %s resources.', $shortName), | |
'returns' => 'hydra:PagedCollection', | |
] + $hydraOperation; | |
} elseif ('GET' === $method) { | |
$hydraOperation = [ | |
'hydra:title' => sprintf('Retrieves %s resource.', $shortName), | |
'returns' => $prefixedShortName, | |
] + $hydraOperation; | |
} elseif ('POST' === $method) { | |
$hydraOperation = [ | |
'@type' => 'hydra:CreateResourceOperation', | |
'hydra:title' => sprintf('Creates a %s resource.', $shortName), | |
'returns' => $prefixedShortName, | |
'expects' => $prefixedShortName, | |
] + $hydraOperation; | |
} elseif ('PUT' === $method) { | |
$hydraOperation = [ | |
'@type' => 'hydra:ReplaceResourceOperation', | |
'hydra:title' => sprintf('Replaces the %s resource.', $shortName), | |
'returns' => $prefixedShortName, | |
'expects' => $prefixedShortName, | |
] + $hydraOperation; | |
} elseif ('DELETE' === $method) { | |
$hydraOperation = [ | |
'hydra:title' => sprintf('Deletes the %s resource.', $shortName), | |
'returns' => 'owl:Nothing', | |
] + $hydraOperation; | |
} | |
$hydraOperation['@type'] ?? $hydraOperation['@type'] = 'hydra:Operation'; | |
$hydraOperation['hydra:method'] ?? $hydraOperation['hydra:method'] = $method; | |
if (!isset($hydraOperation['rdfs:label']) && isset($hydraOperation['hydra:title'])) { | |
$hydraOperation['rdfs:label'] = $hydraOperation['hydra:title']; | |
} | |
ksort($hydraOperation); | |
return $hydraOperation; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great input! I did not think of that, I updated the pull request.
Anyway, please add some tests. 😄 |
I will add some tests, probably I'll need some time to have a look at the existing tests. What about the docs, do you update them or shall I create a pull request there too? |
'@id' => $id, | ||
'@type' => '@id', | ||
]; | ||
} else { | ||
] + $jsonldContext; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverse the order so that what's set in jsonld_context
takes precedence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I have to take care of the deps='low' build in that case? |
This should also address #731 @albertedevigo What do you think? |
@teohhanhui What about the unit tests? |
You should add a test for the |
@albertedevigo @teohhanhui I added a test for I think #731 can be addressed with this issue. However I think there is also a need for a jsonld context on a resource level. I have created an issue here api-platform/api-platform#93 and now moved to #767. |
Thank you @maechler! Can you open a doc PR on https://github.com/api-platform/docs/ to document this new feature please? |
@dunglas Awesome, thanks! |
I think that it deserves a new chapter. |
Adds jsonld context, Adds support for jsonld typed values
This pull request is just an implementation proposal, it is not yet ready to be merged. I have not put any effort into fixing the tests.
This pull request wants to add json-ld typed values: https://www.w3.org/TR/json-ld/#typed-values
e.g.
entity
context
What do you think?