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): store and use hydra context in a local variable #6765

Merged
Merged
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
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 <[email protected]>
*/
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
Loading