Skip to content

Commit cd87caf

Browse files
committed
fix(jsonld): prefix error @type with hydra:
1 parent a11c213 commit cd87caf

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

features/main/relation.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ Feature: Relations support
493493
"properties": {
494494
"@type": {
495495
"type": "string",
496-
"pattern": "^Error$"
496+
"pattern": "^hydra:Error$"
497497
},
498498
"title": {
499499
"type": "string",

features/serializer/vo_relations.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Feature: Value object as ApiResource
148148
"properties": {
149149
"@type": {
150150
"type": "string",
151-
"pattern": "^Error$"
151+
"pattern": "^hydra:Error$"
152152
},
153153
"title": {
154154
"type": "string",

src/JsonLd/Serializer/ErrorNormalizer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ public function __construct(private readonly NormalizerInterface $inner, private
2828

2929
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
3030
{
31+
$context += $this->defaultContext;
3132
$normalized = $this->inner->normalize($object, $format, $context);
32-
$hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
33+
$hydraPrefix = $this->getHydraPrefix($context);
3334
if (!$hydraPrefix) {
3435
return $normalized;
3536
}
3637

38+
if ('Error' === $normalized['@type']) {
39+
$normalized['@type'] = 'hydra:Error';
40+
}
41+
3742
if (isset($normalized['description'])) {
3843
$normalized['hydra:description'] = $normalized['description'];
3944
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\JsonLd\Serializer;
15+
16+
use ApiPlatform\JsonLd\ContextBuilder;
17+
use ApiPlatform\JsonLd\Serializer\ErrorNormalizer;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20+
21+
final class ErrorNormalizerTest extends TestCase
22+
{
23+
public function testAddHydraPrefix(): void
24+
{
25+
$provider = $this->createMock(NormalizerInterface::class);
26+
$provider->method('normalize')->willReturn(['@type' => 'Error', 'title' => 'foo', 'description' => 'bar']);
27+
$errorNormalizer = new ErrorNormalizer($provider, ['hydra_prefix' => ContextBuilder::HYDRA_CONTEXT_HAS_PREFIX]);
28+
$res = $errorNormalizer->normalize(new \stdClass());
29+
$this->assertEquals('hydra:Error', $res['@type']);
30+
$this->assertArrayHasKey('hydra:description', $res);
31+
$this->assertEquals($res['hydra:description'], $res['description']);
32+
$this->assertArrayHasKey('hydra:title', $res);
33+
$this->assertEquals($res['hydra:title'], $res['title']);
34+
}
35+
}

0 commit comments

Comments
 (0)