Skip to content
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

fix(hydra): hydra context changed #6710

Open
wants to merge 1 commit into
base: 4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/JsonLd/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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
Loading