Skip to content

Commit

Permalink
fix(jsonld): prefix error @type with hydra:
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 14, 2024
1 parent a11c213 commit cd87caf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion features/main/relation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ Feature: Relations support
"properties": {
"@type": {
"type": "string",
"pattern": "^Error$"
"pattern": "^hydra:Error$"
},
"title": {
"type": "string",
Expand Down
2 changes: 1 addition & 1 deletion features/serializer/vo_relations.feature
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Feature: Value object as ApiResource
"properties": {
"@type": {
"type": "string",
"pattern": "^Error$"
"pattern": "^hydra:Error$"
},
"title": {
"type": "string",
Expand Down
7 changes: 6 additions & 1 deletion src/JsonLd/Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand Down
35 changes: 35 additions & 0 deletions tests/JsonLd/Serializer/ErrorNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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']);
}
}

0 comments on commit cd87caf

Please sign in to comment.