Skip to content

Commit 6bd6b56

Browse files
fix(hydra): hydra context changed
1 parent ec6e645 commit 6bd6b56

File tree

5 files changed

+122
-108
lines changed

5 files changed

+122
-108
lines changed

features/hydra/docs.feature

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@ Feature: Documentation support
1313
And the response should be in JSON
1414
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
1515
# Context
16-
And the JSON node "@context.@vocab" should be equal to "http://example.com/docs.jsonld#"
17-
And the JSON node "@context.hydra" should be equal to "http://www.w3.org/ns/hydra/core#"
18-
And the JSON node "@context.rdf" should be equal to "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
19-
And the JSON node "@context.rdfs" should be equal to "http://www.w3.org/2000/01/rdf-schema#"
20-
And the JSON node "@context.xmls" should be equal to "http://www.w3.org/2001/XMLSchema#"
21-
And the JSON node "@context.owl" should be equal to "http://www.w3.org/2002/07/owl#"
22-
And the JSON node "@context.domain.@id" should be equal to "rdfs:domain"
23-
And the JSON node "@context.domain.@type" should be equal to "@id"
24-
And the JSON node "@context.range.@id" should be equal to "rdfs:range"
25-
And the JSON node "@context.range.@type" should be equal to "@id"
26-
And the JSON node "@context.subClassOf.@id" should be equal to "rdfs:subClassOf"
27-
And the JSON node "@context.subClassOf.@type" should be equal to "@id"
28-
And the JSON node "@context.expects.@id" should be equal to "hydra:expects"
29-
And the JSON node "@context.expects.@type" should be equal to "@id"
30-
And the JSON node "@context.returns.@id" should be equal to "hydra:returns"
31-
And the JSON node "@context.returns.@type" should be equal to "@id"
16+
And the JSON node "@context[0]" should be equal to "http://www.w3.org/ns/hydra/context.jsonld"
17+
And the JSON node "@context[1].@vocab" should be equal to "http://example.com/docs.jsonld#"
18+
And the JSON node "@context[1].hydra" should be equal to "http://www.w3.org/ns/hydra/core#"
19+
And the JSON node "@context[1].rdf" should be equal to "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
20+
And the JSON node "@context[1].rdfs" should be equal to "http://www.w3.org/2000/01/rdf-schema#"
21+
And the JSON node "@context[1].xmls" should be equal to "http://www.w3.org/2001/XMLSchema#"
22+
And the JSON node "@context[1].owl" should be equal to "http://www.w3.org/2002/07/owl#"
23+
And the JSON node "@context[1].domain.@id" should be equal to "rdfs:domain"
24+
And the JSON node "@context[1].domain.@type" should be equal to "@id"
25+
And the JSON node "@context[1].range.@id" should be equal to "rdfs:range"
26+
And the JSON node "@context[1].range.@type" should be equal to "@id"
27+
And the JSON node "@context[1].subClassOf.@id" should be equal to "rdfs:subClassOf"
28+
And the JSON node "@context[1].subClassOf.@type" should be equal to "@id"
29+
And the JSON node "@context[1].expects.@id" should be equal to "hydra:expects"
30+
And the JSON node "@context[1].expects.@type" should be equal to "@id"
31+
And the JSON node "@context[1].returns.@id" should be equal to "hydra:returns"
32+
And the JSON node "@context[1].returns.@type" should be equal to "@id"
3233
# Root properties
3334
And the JSON node "@id" should be equal to "/docs.jsonld"
3435
And the JSON node "hydra:title" should be equal to "My Dummy API"

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -573,18 +573,21 @@ private function computeDoc(Documentation $object, array $classes, string $hydra
573573
private function getContext(string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
574574
{
575575
return [
576-
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
577-
'hydra' => ContextBuilderInterface::HYDRA_NS,
578-
'rdf' => ContextBuilderInterface::RDF_NS,
579-
'rdfs' => ContextBuilderInterface::RDFS_NS,
580-
'xmls' => ContextBuilderInterface::XML_NS,
581-
'owl' => ContextBuilderInterface::OWL_NS,
582-
'schema' => ContextBuilderInterface::SCHEMA_ORG_NS,
583-
'domain' => ['@id' => 'rdfs:domain', '@type' => '@id'],
584-
'range' => ['@id' => 'rdfs:range', '@type' => '@id'],
585-
'subClassOf' => ['@id' => 'rdfs:subClassOf', '@type' => '@id'],
586-
'expects' => ['@id' => $hydraPrefix.'expects', '@type' => '@id'],
587-
'returns' => ['@id' => $hydraPrefix.'returns', '@type' => '@id'],
576+
ContextBuilderInterface::HYDRA_CONTEXT,
577+
[
578+
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
579+
'hydra' => ContextBuilderInterface::HYDRA_NS,
580+
'rdf' => ContextBuilderInterface::RDF_NS,
581+
'rdfs' => ContextBuilderInterface::RDFS_NS,
582+
'xmls' => ContextBuilderInterface::XML_NS,
583+
'owl' => ContextBuilderInterface::OWL_NS,
584+
'schema' => ContextBuilderInterface::SCHEMA_ORG_NS,
585+
'domain' => ['@id' => 'rdfs:domain', '@type' => '@id'],
586+
'range' => ['@id' => 'rdfs:range', '@type' => '@id'],
587+
'subClassOf' => ['@id' => 'rdfs:subClassOf', '@type' => '@id'],
588+
'expects' => ['@id' => $hydraPrefix.'expects', '@type' => '@id'],
589+
'returns' => ['@id' => $hydraPrefix.'returns', '@type' => '@id'],
590+
],
588591
];
589592
}
590593

src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php

Lines changed: 87 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,35 @@ private function doTestNormalize($resourceMetadataFactory = null): void
106106

107107
$expected = [
108108
'@context' => [
109-
'@vocab' => '/doc#',
110-
'hydra' => 'http://www.w3.org/ns/hydra/core#',
111-
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
112-
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
113-
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
114-
'owl' => 'http://www.w3.org/2002/07/owl#',
115-
'schema' => 'https://schema.org/',
116-
'domain' => [
117-
'@id' => 'rdfs:domain',
118-
'@type' => '@id',
119-
],
120-
'range' => [
121-
'@id' => 'rdfs:range',
122-
'@type' => '@id',
123-
],
124-
'subClassOf' => [
125-
'@id' => 'rdfs:subClassOf',
126-
'@type' => '@id',
127-
],
128-
'expects' => [
129-
'@id' => 'hydra:expects',
130-
'@type' => '@id',
131-
],
132-
'returns' => [
133-
'@id' => 'hydra:returns',
134-
'@type' => '@id',
109+
'http://www.w3.org/ns/hydra/context.jsonld',
110+
[
111+
'@vocab' => '/doc#',
112+
'hydra' => 'http://www.w3.org/ns/hydra/core#',
113+
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
114+
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
115+
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
116+
'owl' => 'http://www.w3.org/2002/07/owl#',
117+
'schema' => 'https://schema.org/',
118+
'domain' => [
119+
'@id' => 'rdfs:domain',
120+
'@type' => '@id',
121+
],
122+
'range' => [
123+
'@id' => 'rdfs:range',
124+
'@type' => '@id',
125+
],
126+
'subClassOf' => [
127+
'@id' => 'rdfs:subClassOf',
128+
'@type' => '@id',
129+
],
130+
'expects' => [
131+
'@id' => 'hydra:expects',
132+
'@type' => '@id',
133+
],
134+
'returns' => [
135+
'@id' => 'hydra:returns',
136+
'@type' => '@id',
137+
],
135138
],
136139
],
137140
'@id' => '/doc',
@@ -411,32 +414,35 @@ public function testNormalizeInputOutputClass(): void
411414

412415
$expected = [
413416
'@context' => [
414-
'@vocab' => '/doc#',
415-
'hydra' => 'http://www.w3.org/ns/hydra/core#',
416-
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
417-
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
418-
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
419-
'owl' => 'http://www.w3.org/2002/07/owl#',
420-
'schema' => 'https://schema.org/',
421-
'domain' => [
422-
'@id' => 'rdfs:domain',
423-
'@type' => '@id',
424-
],
425-
'range' => [
426-
'@id' => 'rdfs:range',
427-
'@type' => '@id',
428-
],
429-
'subClassOf' => [
430-
'@id' => 'rdfs:subClassOf',
431-
'@type' => '@id',
432-
],
433-
'expects' => [
434-
'@id' => 'hydra:expects',
435-
'@type' => '@id',
436-
],
437-
'returns' => [
438-
'@id' => 'hydra:returns',
439-
'@type' => '@id',
417+
'http://www.w3.org/ns/hydra/context.jsonld',
418+
[
419+
'@vocab' => '/doc#',
420+
'hydra' => 'http://www.w3.org/ns/hydra/core#',
421+
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
422+
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
423+
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
424+
'owl' => 'http://www.w3.org/2002/07/owl#',
425+
'schema' => 'https://schema.org/',
426+
'domain' => [
427+
'@id' => 'rdfs:domain',
428+
'@type' => '@id',
429+
],
430+
'range' => [
431+
'@id' => 'rdfs:range',
432+
'@type' => '@id',
433+
],
434+
'subClassOf' => [
435+
'@id' => 'rdfs:subClassOf',
436+
'@type' => '@id',
437+
],
438+
'expects' => [
439+
'@id' => 'hydra:expects',
440+
'@type' => '@id',
441+
],
442+
'returns' => [
443+
'@id' => 'hydra:returns',
444+
'@type' => '@id',
445+
],
440446
],
441447
],
442448
'@id' => '/doc',
@@ -776,32 +782,35 @@ public function testNormalizeWithoutPrefix(): void
776782

777783
$expected = [
778784
'@context' => [
779-
'@vocab' => '/doc#',
780-
'hydra' => 'http://www.w3.org/ns/hydra/core#',
781-
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
782-
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
783-
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
784-
'owl' => 'http://www.w3.org/2002/07/owl#',
785-
'schema' => 'https://schema.org/',
786-
'domain' => [
787-
'@id' => 'rdfs:domain',
788-
'@type' => '@id',
789-
],
790-
'range' => [
791-
'@id' => 'rdfs:range',
792-
'@type' => '@id',
793-
],
794-
'subClassOf' => [
795-
'@id' => 'rdfs:subClassOf',
796-
'@type' => '@id',
797-
],
798-
'expects' => [
799-
'@id' => 'expects',
800-
'@type' => '@id',
801-
],
802-
'returns' => [
803-
'@id' => 'returns',
804-
'@type' => '@id',
785+
'http://www.w3.org/ns/hydra/context.jsonld',
786+
[
787+
'@vocab' => '/doc#',
788+
'hydra' => 'http://www.w3.org/ns/hydra/core#',
789+
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
790+
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
791+
'xmls' => 'http://www.w3.org/2001/XMLSchema#',
792+
'owl' => 'http://www.w3.org/2002/07/owl#',
793+
'schema' => 'https://schema.org/',
794+
'domain' => [
795+
'@id' => 'rdfs:domain',
796+
'@type' => '@id',
797+
],
798+
'range' => [
799+
'@id' => 'rdfs:range',
800+
'@type' => '@id',
801+
],
802+
'subClassOf' => [
803+
'@id' => 'rdfs:subClassOf',
804+
'@type' => '@id',
805+
],
806+
'expects' => [
807+
'@id' => 'expects',
808+
'@type' => '@id',
809+
],
810+
'returns' => [
811+
'@id' => 'returns',
812+
'@type' => '@id',
813+
],
805814
],
806815
],
807816
'@id' => '/doc',

src/JsonLd/ContextBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ private function getResourceContextWithShortname(string $resourceClass, int $ref
185185
}
186186
}
187187

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

192192
return $context;

src/JsonLd/ContextBuilderInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
interface ContextBuilderInterface
2525
{
26+
public const HYDRA_CONTEXT = 'http://www.w3.org/ns/hydra/context.jsonld';
2627
public const HYDRA_NS = 'http://www.w3.org/ns/hydra/core#';
2728
public const JSONLD_NS = 'http://www.w3.org/ns/json-ld#';
2829
public const RDF_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';

0 commit comments

Comments
 (0)