From b611059acc9758d01d3c99a838380f0ed580e7c0 Mon Sep 17 00:00:00 2001 From: soyuka Date: Mon, 14 Oct 2024 09:42:07 +0200 Subject: [PATCH] fix(jsonld): prefix error @type with hydra: --- src/JsonLd/Serializer/ErrorNormalizer.php | 7 +++++- .../JsonLd/Serializer/ErrorNormalizerTest.php | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/JsonLd/Serializer/ErrorNormalizerTest.php diff --git a/src/JsonLd/Serializer/ErrorNormalizer.php b/src/JsonLd/Serializer/ErrorNormalizer.php index b5bf3eec6c6..caae889a799 100644 --- a/src/JsonLd/Serializer/ErrorNormalizer.php +++ b/src/JsonLd/Serializer/ErrorNormalizer.php @@ -28,12 +28,17 @@ public function __construct(private readonly NormalizerInterface $inner, private public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null { + $context = $context + $this->defaultContext; $normalized = $this->inner->normalize($object, $format, $context); - $hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext); + $hydraPrefix = $this->getHydraPrefix($context); if (!$hydraPrefix) { return $normalized; } + if ('Error' === $normalized['@type']) { + $normalized['@type'] = 'hydra:Error'; + } + if (isset($normalized['description'])) { $normalized['hydra:description'] = $normalized['description']; } diff --git a/tests/JsonLd/Serializer/ErrorNormalizerTest.php b/tests/JsonLd/Serializer/ErrorNormalizerTest.php new file mode 100644 index 00000000000..36cd608236d --- /dev/null +++ b/tests/JsonLd/Serializer/ErrorNormalizerTest.php @@ -0,0 +1,22 @@ +createMock(NormalizerInterface::class); + $provider->method('normalize')->willReturn(['@type' => 'Error', 'title' => 'foo', 'description' => 'bar']); + $errorNormalizer = new ErrorNormalizer($provider, ['hydra_prefix' => ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX]); + $res = $errorNormalizer->normalize(new \stdClass()); + $this->assertEquals('hydra:Error', $res['@type']); + $this->assertEquals('hydra:description', $res['description']); + $this->assertEquals('hydra:title', $res['title']); + } +}