Skip to content

Commit

Permalink
fix(hydra): hydra context changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Dassonville committed Oct 11, 2024
1 parent ec6e645 commit 6bd6b56
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 108 deletions.
33 changes: 17 additions & 16 deletions features/hydra/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ Feature: Documentation support
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
# Context
And the JSON node "@context.@vocab" should be equal to "http://example.com/docs.jsonld#"
And the JSON node "@context.hydra" should be equal to "http://www.w3.org/ns/hydra/core#"
And the JSON node "@context.rdf" should be equal to "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
And the JSON node "@context.rdfs" should be equal to "http://www.w3.org/2000/01/rdf-schema#"
And the JSON node "@context.xmls" should be equal to "http://www.w3.org/2001/XMLSchema#"
And the JSON node "@context.owl" should be equal to "http://www.w3.org/2002/07/owl#"
And the JSON node "@context.domain.@id" should be equal to "rdfs:domain"
And the JSON node "@context.domain.@type" should be equal to "@id"
And the JSON node "@context.range.@id" should be equal to "rdfs:range"
And the JSON node "@context.range.@type" should be equal to "@id"
And the JSON node "@context.subClassOf.@id" should be equal to "rdfs:subClassOf"
And the JSON node "@context.subClassOf.@type" should be equal to "@id"
And the JSON node "@context.expects.@id" should be equal to "hydra:expects"
And the JSON node "@context.expects.@type" should be equal to "@id"
And the JSON node "@context.returns.@id" should be equal to "hydra:returns"
And the JSON node "@context.returns.@type" should be equal to "@id"
And the JSON node "@context[0]" should be equal to "http://www.w3.org/ns/hydra/context.jsonld"
And the JSON node "@context[1].@vocab" should be equal to "http://example.com/docs.jsonld#"
And the JSON node "@context[1].hydra" should be equal to "http://www.w3.org/ns/hydra/core#"
And the JSON node "@context[1].rdf" should be equal to "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
And the JSON node "@context[1].rdfs" should be equal to "http://www.w3.org/2000/01/rdf-schema#"
And the JSON node "@context[1].xmls" should be equal to "http://www.w3.org/2001/XMLSchema#"
And the JSON node "@context[1].owl" should be equal to "http://www.w3.org/2002/07/owl#"
And the JSON node "@context[1].domain.@id" should be equal to "rdfs:domain"
And the JSON node "@context[1].domain.@type" should be equal to "@id"
And the JSON node "@context[1].range.@id" should be equal to "rdfs:range"
And the JSON node "@context[1].range.@type" should be equal to "@id"
And the JSON node "@context[1].subClassOf.@id" should be equal to "rdfs:subClassOf"
And the JSON node "@context[1].subClassOf.@type" should be equal to "@id"
And the JSON node "@context[1].expects.@id" should be equal to "hydra:expects"
And the JSON node "@context[1].expects.@type" should be equal to "@id"
And the JSON node "@context[1].returns.@id" should be equal to "hydra:returns"
And the JSON node "@context[1].returns.@type" should be equal to "@id"
# Root properties
And the JSON node "@id" should be equal to "/docs.jsonld"
And the JSON node "hydra:title" should be equal to "My Dummy API"
Expand Down
27 changes: 15 additions & 12 deletions src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,18 +573,21 @@ private function computeDoc(Documentation $object, array $classes, string $hydra
private function getContext(string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
{
return [
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
'hydra' => ContextBuilderInterface::HYDRA_NS,
'rdf' => ContextBuilderInterface::RDF_NS,
'rdfs' => ContextBuilderInterface::RDFS_NS,
'xmls' => ContextBuilderInterface::XML_NS,
'owl' => ContextBuilderInterface::OWL_NS,
'schema' => ContextBuilderInterface::SCHEMA_ORG_NS,
'domain' => ['@id' => 'rdfs:domain', '@type' => '@id'],
'range' => ['@id' => 'rdfs:range', '@type' => '@id'],
'subClassOf' => ['@id' => 'rdfs:subClassOf', '@type' => '@id'],
'expects' => ['@id' => $hydraPrefix.'expects', '@type' => '@id'],
'returns' => ['@id' => $hydraPrefix.'returns', '@type' => '@id'],
ContextBuilderInterface::HYDRA_CONTEXT,
[
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
'hydra' => ContextBuilderInterface::HYDRA_NS,
'rdf' => ContextBuilderInterface::RDF_NS,
'rdfs' => ContextBuilderInterface::RDFS_NS,
'xmls' => ContextBuilderInterface::XML_NS,
'owl' => ContextBuilderInterface::OWL_NS,
'schema' => ContextBuilderInterface::SCHEMA_ORG_NS,
'domain' => ['@id' => 'rdfs:domain', '@type' => '@id'],
'range' => ['@id' => 'rdfs:range', '@type' => '@id'],
'subClassOf' => ['@id' => 'rdfs:subClassOf', '@type' => '@id'],
'expects' => ['@id' => $hydraPrefix.'expects', '@type' => '@id'],
'returns' => ['@id' => $hydraPrefix.'returns', '@type' => '@id'],
],
];
}

Expand Down
165 changes: 87 additions & 78 deletions src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,35 @@ private function doTestNormalize($resourceMetadataFactory = null): void

$expected = [
'@context' => [
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'schema' => 'https://schema.org/',
'domain' => [
'@id' => 'rdfs:domain',
'@type' => '@id',
],
'range' => [
'@id' => 'rdfs:range',
'@type' => '@id',
],
'subClassOf' => [
'@id' => 'rdfs:subClassOf',
'@type' => '@id',
],
'expects' => [
'@id' => 'hydra:expects',
'@type' => '@id',
],
'returns' => [
'@id' => 'hydra:returns',
'@type' => '@id',
'http://www.w3.org/ns/hydra/context.jsonld',
[
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'schema' => 'https://schema.org/',
'domain' => [
'@id' => 'rdfs:domain',
'@type' => '@id',
],
'range' => [
'@id' => 'rdfs:range',
'@type' => '@id',
],
'subClassOf' => [
'@id' => 'rdfs:subClassOf',
'@type' => '@id',
],
'expects' => [
'@id' => 'hydra:expects',
'@type' => '@id',
],
'returns' => [
'@id' => 'hydra:returns',
'@type' => '@id',
],
],
],
'@id' => '/doc',
Expand Down Expand Up @@ -411,32 +414,35 @@ public function testNormalizeInputOutputClass(): void

$expected = [
'@context' => [
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'schema' => 'https://schema.org/',
'domain' => [
'@id' => 'rdfs:domain',
'@type' => '@id',
],
'range' => [
'@id' => 'rdfs:range',
'@type' => '@id',
],
'subClassOf' => [
'@id' => 'rdfs:subClassOf',
'@type' => '@id',
],
'expects' => [
'@id' => 'hydra:expects',
'@type' => '@id',
],
'returns' => [
'@id' => 'hydra:returns',
'@type' => '@id',
'http://www.w3.org/ns/hydra/context.jsonld',
[
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'schema' => 'https://schema.org/',
'domain' => [
'@id' => 'rdfs:domain',
'@type' => '@id',
],
'range' => [
'@id' => 'rdfs:range',
'@type' => '@id',
],
'subClassOf' => [
'@id' => 'rdfs:subClassOf',
'@type' => '@id',
],
'expects' => [
'@id' => 'hydra:expects',
'@type' => '@id',
],
'returns' => [
'@id' => 'hydra:returns',
'@type' => '@id',
],
],
],
'@id' => '/doc',
Expand Down Expand Up @@ -776,32 +782,35 @@ public function testNormalizeWithoutPrefix(): void

$expected = [
'@context' => [
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'schema' => 'https://schema.org/',
'domain' => [
'@id' => 'rdfs:domain',
'@type' => '@id',
],
'range' => [
'@id' => 'rdfs:range',
'@type' => '@id',
],
'subClassOf' => [
'@id' => 'rdfs:subClassOf',
'@type' => '@id',
],
'expects' => [
'@id' => 'expects',
'@type' => '@id',
],
'returns' => [
'@id' => 'returns',
'@type' => '@id',
'http://www.w3.org/ns/hydra/context.jsonld',
[
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'schema' => 'https://schema.org/',
'domain' => [
'@id' => 'rdfs:domain',
'@type' => '@id',
],
'range' => [
'@id' => 'rdfs:range',
'@type' => '@id',
],
'subClassOf' => [
'@id' => 'rdfs:subClassOf',
'@type' => '@id',
],
'expects' => [
'@id' => 'expects',
'@type' => '@id',
],
'returns' => [
'@id' => 'returns',
'@type' => '@id',
],
],
],
'@id' => '/doc',
Expand Down
4 changes: 2 additions & 2 deletions src/JsonLd/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ private function getResourceContextWithShortname(string $resourceClass, int $ref
}
}

if (false === ($this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true) || $operation instanceof Error) {
return ['http://www.w3.org/ns/hydra/context.jsonld', $context];
if (false === ($this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true)) {
return [ContextBuilderInterface::HYDRA_CONTEXT, $context];
}

return $context;
Expand Down
1 change: 1 addition & 0 deletions src/JsonLd/ContextBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
interface ContextBuilderInterface
{
public const HYDRA_CONTEXT = 'http://www.w3.org/ns/hydra/context.jsonld';
public const HYDRA_NS = 'http://www.w3.org/ns/hydra/core#';
public const JSONLD_NS = 'http://www.w3.org/ns/json-ld#';
public const RDF_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
Expand Down

0 comments on commit 6bd6b56

Please sign in to comment.