Skip to content

Commit 9ac3661

Browse files
valentin-dassonvilledunglassoyuka
authored
fix(hydra): store and use hydra context in a local variable (#6765)
Co-authored-by: Kévin Dunglas <[email protected]> Co-authored-by: soyuka <[email protected]>
1 parent 6c9b508 commit 9ac3661

File tree

10 files changed

+993
-8
lines changed

10 files changed

+993
-8
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"autoload": {
1010
"psr-4": {
1111
"ApiPlatform\\": "src/"
12-
}
12+
},
13+
"files": [
14+
"src/JsonLd/HydraContext.php"
15+
]
1316
},
1417
"autoload-dev": {
1518
"psr-4": {

features/hydra/docs.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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[0]" should be equal to "http://www.w3.org/ns/hydra/context.jsonld"
16+
And the Hydra context matches the online resource "http://www.w3.org/ns/hydra/context.jsonld"
1717
And the JSON node "@context[1].@vocab" should be equal to "http://example.com/docs.jsonld#"
1818
And the JSON node "@context[1].hydra" should be equal to "http://www.w3.org/ns/hydra/core#"
1919
And the JSON node "@context[1].rdf" should be equal to "http://www.w3.org/1999/02/22-rdf-syntax-ns#"

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3636
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
3737

38+
use const ApiPlatform\JsonLd\HYDRA_CONTEXT;
39+
3840
/**
3941
* Creates a machine readable Hydra API documentation.
4042
*
@@ -573,7 +575,7 @@ private function computeDoc(Documentation $object, array $classes, string $hydra
573575
private function getContext(string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
574576
{
575577
return [
576-
ContextBuilderInterface::HYDRA_CONTEXT,
578+
HYDRA_CONTEXT,
577579
[
578580
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
579581
'hydra' => ContextBuilderInterface::HYDRA_NS,

src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
use Prophecy\PhpUnit\ProphecyTrait;
3838
use Symfony\Component\PropertyInfo\Type;
3939

40+
use const ApiPlatform\JsonLd\HYDRA_CONTEXT;
41+
4042
/**
4143
* @author Amrouche Hamza <[email protected]>
4244
*/
@@ -106,7 +108,7 @@ private function doTestNormalize($resourceMetadataFactory = null): void
106108

107109
$expected = [
108110
'@context' => [
109-
'http://www.w3.org/ns/hydra/context.jsonld',
111+
HYDRA_CONTEXT,
110112
[
111113
'@vocab' => '/doc#',
112114
'hydra' => 'http://www.w3.org/ns/hydra/core#',
@@ -406,7 +408,7 @@ public function testNormalizeInputOutputClass(): void
406408

407409
$expected = [
408410
'@context' => [
409-
'http://www.w3.org/ns/hydra/context.jsonld',
411+
HYDRA_CONTEXT,
410412
[
411413
'@vocab' => '/doc#',
412414
'hydra' => 'http://www.w3.org/ns/hydra/core#',
@@ -766,7 +768,7 @@ public function testNormalizeWithoutPrefix(): void
766768

767769
$expected = [
768770
'@context' => [
769-
'http://www.w3.org/ns/hydra/context.jsonld',
771+
HYDRA_CONTEXT,
770772
[
771773
'@vocab' => '/doc#',
772774
'hydra' => 'http://www.w3.org/ns/hydra/core#',

src/JsonLd/ContextBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private function getResourceContextWithShortname(string $resourceClass, int $ref
185185
}
186186

187187
if (false === ($this->defaultContext[self::HYDRA_CONTEXT_HAS_PREFIX] ?? true)) {
188-
return [ContextBuilderInterface::HYDRA_CONTEXT, $context];
188+
return [HYDRA_CONTEXT, $context];
189189
}
190190

191191
return $context;

0 commit comments

Comments
 (0)