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 d8b493d
Show file tree
Hide file tree
Showing 4 changed files with 32 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
24 changes: 24 additions & 0 deletions tests/JsonLd/Serializer/ErrorNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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()
{
$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 d8b493d

Please sign in to comment.