diff --git a/features/hydra/docs.feature b/features/hydra/docs.feature index d848ac5596..17784b0a9c 100644 --- a/features/hydra/docs.feature +++ b/features/hydra/docs.feature @@ -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" diff --git a/src/Hydra/Serializer/DocumentationNormalizer.php b/src/Hydra/Serializer/DocumentationNormalizer.php index f27a1a1020..9520115594 100644 --- a/src/Hydra/Serializer/DocumentationNormalizer.php +++ b/src/Hydra/Serializer/DocumentationNormalizer.php @@ -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'], + ], ]; } diff --git a/src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php b/src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php index b3e8b4a1f7..1521db5cfb 100644 --- a/src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php +++ b/src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php @@ -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', @@ -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', @@ -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', diff --git a/src/JsonLd/ContextBuilder.php b/src/JsonLd/ContextBuilder.php index 0fe1063636..31b9172c98 100644 --- a/src/JsonLd/ContextBuilder.php +++ b/src/JsonLd/ContextBuilder.php @@ -186,7 +186,7 @@ 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]; + return [ContextBuilderInterface::HYDRA_CONTEXT, $context]; } return $context; diff --git a/src/JsonLd/ContextBuilderInterface.php b/src/JsonLd/ContextBuilderInterface.php index 63c34d66c4..c90152c1f8 100644 --- a/src/JsonLd/ContextBuilderInterface.php +++ b/src/JsonLd/ContextBuilderInterface.php @@ -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#';