From cd87caf5b613349a7e08cd4da6058e591366e54a 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: --- features/main/relation.feature | 2 +- features/serializer/vo_relations.feature | 2 +- src/JsonLd/Serializer/ErrorNormalizer.php | 7 +++- .../JsonLd/Serializer/ErrorNormalizerTest.php | 35 +++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/JsonLd/Serializer/ErrorNormalizerTest.php diff --git a/features/main/relation.feature b/features/main/relation.feature index 206a4789ffe..0f16560dfbe 100644 --- a/features/main/relation.feature +++ b/features/main/relation.feature @@ -493,7 +493,7 @@ Feature: Relations support "properties": { "@type": { "type": "string", - "pattern": "^Error$" + "pattern": "^hydra:Error$" }, "title": { "type": "string", diff --git a/features/serializer/vo_relations.feature b/features/serializer/vo_relations.feature index ccf49439814..63600d7b8a1 100644 --- a/features/serializer/vo_relations.feature +++ b/features/serializer/vo_relations.feature @@ -148,7 +148,7 @@ Feature: Value object as ApiResource "properties": { "@type": { "type": "string", - "pattern": "^Error$" + "pattern": "^hydra:Error$" }, "title": { "type": "string", diff --git a/src/JsonLd/Serializer/ErrorNormalizer.php b/src/JsonLd/Serializer/ErrorNormalizer.php index b5bf3eec6c6..d1b27e9d3f0 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 += $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..916217d409d --- /dev/null +++ b/tests/JsonLd/Serializer/ErrorNormalizerTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Tests\JsonLd\Serializer; + +use ApiPlatform\JsonLd\ContextBuilder; +use ApiPlatform\JsonLd\Serializer\ErrorNormalizer; +use PHPUnit\Framework\TestCase; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; + +final class ErrorNormalizerTest extends TestCase +{ + public function testAddHydraPrefix(): void + { + $provider = $this->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->assertArrayHasKey('hydra:description', $res); + $this->assertEquals($res['hydra:description'], $res['description']); + $this->assertArrayHasKey('hydra:title', $res); + $this->assertEquals($res['hydra:title'], $res['title']); + } +}