Skip to content

Commit 9ac3661

Browse files
valentin-dassonvilledunglassoyuka
authoredNov 6, 2024··
fix(hydra): store and use hydra context in a local variable (#6765)
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> Co-authored-by: soyuka <soyuka@users.noreply.github.com>
1 parent 6c9b508 commit 9ac3661

10 files changed

+993
-8
lines changed
 

‎composer.json

+4-1
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

+1-1
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

+3-1
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

+5-3
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 <hamza.simperfit@gmail.com>
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

+1-1
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;

‎src/JsonLd/HydraContext.php

+919
Large diffs are not rendered by default.

‎src/JsonLd/composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
"psr-4": {
3333
"ApiPlatform\\JsonLd\\": ""
3434
},
35+
"files": [
36+
"./HydraContext.php"
37+
],
3538
"exclude-from-classmap": [
3639
"/Tests/"
3740
]

‎tests/Behat/HydraContext.php

+22
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,26 @@ private function getLastJsonResponse(): \stdClass
301301

302302
return $decoded;
303303
}
304+
305+
/**
306+
* @Then the Hydra context matches the online resource :url
307+
*/
308+
public function assertHydraContextIsCorrect(string $url): void
309+
{
310+
$opts = [
311+
'http' => [
312+
'method' => 'GET',
313+
'header' => "User-Agent: Mozilla/5.0\r\n",
314+
],
315+
];
316+
317+
$context = stream_context_create($opts);
318+
$upstream = json_decode(file_get_contents($url, false, $context));
319+
$actual = $this->getLastJsonResponse();
320+
$local = $actual->{'@context'}[0];
321+
Assert::assertEquals(
322+
$upstream,
323+
$local
324+
);
325+
}
304326
}

‎tests/JsonLd/ContextBuilderTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use Prophecy\Prophecy\ObjectProphecy;
3636
use Symfony\Component\PropertyInfo\Type;
3737

38+
use const ApiPlatform\JsonLd\HYDRA_CONTEXT;
39+
3840
/**
3941
* @author Markus Mächler <markus.maechler@bithost.ch>
4042
*/
@@ -291,7 +293,7 @@ public function testResourceContextWithoutHydraPrefix(): void
291293
$contextBuilder = new ContextBuilder($this->resourceNameCollectionFactoryProphecy->reveal(), $this->resourceMetadataCollectionFactoryProphecy->reveal(), $this->propertyNameCollectionFactoryProphecy->reveal(), $this->propertyMetadataFactoryProphecy->reveal(), $this->urlGeneratorProphecy->reveal(), null, null, [ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX => false]);
292294

293295
$expected = [
294-
'http://www.w3.org/ns/hydra/context.jsonld',
296+
HYDRA_CONTEXT,
295297
[
296298
'@vocab' => '#',
297299
'hydra' => 'http://www.w3.org/ns/hydra/core#',

‎update-hydra-context.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
$opts = [
15+
'http' => [
16+
'method' => 'GET',
17+
'header' => "User-Agent: Mozilla/5.0\r\n",
18+
],
19+
];
20+
21+
$context = stream_context_create($opts);
22+
$hydraContext = json_decode(file_get_contents('http://www.w3.org/ns/hydra/context.jsonld', false, $context), true);
23+
file_put_contents('src/JsonLd/HydraContext.php', '<?php
24+
declare(strict_types=1);
25+
26+
namespace ApiPlatform\JsonLd;
27+
28+
/*
29+
* This is an autogenerated file, DO NOT MODIFY IT.
30+
* Run the update-hydra-context.php script at the root of the project to refresh it.
31+
*/
32+
const HYDRA_CONTEXT = '.var_export($hydraContext, true).';');

0 commit comments

Comments
 (0)
Please sign in to comment.