Skip to content

Commit

Permalink
fix(hydra): store and use hydra context as static asset
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-dassonville committed Oct 30, 2024
1 parent 001d771 commit 97db06a
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 8 deletions.
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 JSON node "@context[0]" should be equal to "/bundles/apiplatform/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
2 changes: 1 addition & 1 deletion src/Hydra/Serializer/DocumentationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ private function computeDoc(Documentation $object, array $classes, string $hydra
private function getContext(string $hydraPrefix = ContextBuilder::HYDRA_PREFIX): array
{
return [
ContextBuilderInterface::HYDRA_CONTEXT,
'/bundles/apiplatform/hydra/context.jsonld',
[
'@vocab' => $this->urlGenerator->generate('api_doc', ['_format' => self::FORMAT], UrlGeneratorInterface::ABS_URL).'#',
'hydra' => ContextBuilderInterface::HYDRA_NS,
Expand Down
6 changes: 3 additions & 3 deletions src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private function doTestNormalize($resourceMetadataFactory = null): void

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

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

$expected = [
'@context' => [
'http://www.w3.org/ns/hydra/context.jsonld',
'/bundles/apiplatform/hydra/context.jsonld',
[
'@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 ['/bundles/apiplatform/hydra/context.jsonld', $context];
}

return $context;
Expand Down
1 change: 0 additions & 1 deletion src/JsonLd/ContextBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
interface ContextBuilderInterface
{
public const HYDRA_CONTEXT = 'http://www.w3.org/ns/hydra/context.jsonld';
public const HYDRA_NS = 'http://www.w3.org/ns/hydra/core#';
public const JSONLD_NS = 'http://www.w3.org/ns/json-ld#';
public const RDF_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
Expand Down
2 changes: 1 addition & 1 deletion tests/JsonLd/ContextBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public function testResourceContextWithoutHydraPrefix(): void
$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]);

$expected = [
'http://www.w3.org/ns/hydra/context.jsonld',
'/bundles/apiplatform/hydra/context.jsonld',
[
'@vocab' => '#',
'hydra' => 'http://www.w3.org/ns/hydra/core#',
Expand Down
7 changes: 7 additions & 0 deletions update-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,12 @@ fi
mkdir -p "$dest"
cp node_modules/redoc/bundles/redoc.standalone.js "$dest"

dest=src/Symfony/Bundle/Resources/public/hydra/
if [[ -d "$dest" ]]; then
rm -Rf "$dest"
fi
mkdir -p "$dest"
curl -L -A "Mozilla/5.0" -o "${dest}context.jsonld" http://www.w3.org/ns/hydra/context.jsonld

rm -Rf package.json node_modules/
# TODO Laravel public files

0 comments on commit 97db06a

Please sign in to comment.