Skip to content

Commit

Permalink
fix(hydra): store and use hydra context in a local variable (#6765)
Browse files Browse the repository at this point in the history
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: soyuka <soyuka@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 6, 2024
1 parent 6c9b508 commit 9ac3661
Show file tree
Hide file tree
Showing 10 changed files with 993 additions and 8 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"autoload": {
"psr-4": {
"ApiPlatform\\": "src/"
}
},
"files": [
"src/JsonLd/HydraContext.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion features/hydra/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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[0]" should be equal to "http://www.w3.org/ns/hydra/context.jsonld"
And the Hydra context matches the online resource "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#"
Expand Down
4 changes: 3 additions & 1 deletion src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

use const ApiPlatform\JsonLd\HYDRA_CONTEXT;

/**
* Creates a machine readable Hydra API documentation.
*
Expand Down Expand Up @@ -573,7 +575,7 @@ private function computeDoc(Documentation $object, array $classes, string $hydra
private function getContext(string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
{
return [
ContextBuilderInterface::HYDRA_CONTEXT,
HYDRA_CONTEXT,
[
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
'hydra' => ContextBuilderInterface::HYDRA_NS,
Expand Down
8 changes: 5 additions & 3 deletions src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\PropertyInfo\Type;

use const ApiPlatform\JsonLd\HYDRA_CONTEXT;

/**
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
*/
Expand Down Expand Up @@ -106,7 +108,7 @@ private function doTestNormalize($resourceMetadataFactory = null): void

$expected = [
'@context' => [
'http://www.w3.org/ns/hydra/context.jsonld',
HYDRA_CONTEXT,
[
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
Expand Down Expand Up @@ -406,7 +408,7 @@ public function testNormalizeInputOutputClass(): void

$expected = [
'@context' => [
'http://www.w3.org/ns/hydra/context.jsonld',
HYDRA_CONTEXT,
[
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
Expand Down Expand Up @@ -766,7 +768,7 @@ public function testNormalizeWithoutPrefix(): void

$expected = [
'@context' => [
'http://www.w3.org/ns/hydra/context.jsonld',
HYDRA_CONTEXT,
[
'@vocab' => '/doc#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
Expand Down
2 changes: 1 addition & 1 deletion src/JsonLd/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private function getResourceContextWithShortname(string $resourceClass, int $ref
}

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

return $context;
Expand Down
Loading

0 comments on commit 9ac3661

Please sign in to comment.