-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
76 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,8 @@ | |
namespace ApiPlatform\JsonApi\Serializer; | ||
|
||
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; | ||
use ApiPlatform\Serializer\CacheableSupportsMethodInterface; | ||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\Serializer; | ||
use Symfony\Component\Validator\ConstraintViolationInterface; | ||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
|
||
|
@@ -26,7 +24,7 @@ | |
* | ||
* @author Héctor Hurtarte <[email protected]> | ||
*/ | ||
final class ConstraintViolationListNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface | ||
final class ConstraintViolationListNormalizer implements NormalizerInterface | ||
{ | ||
public const FORMAT = 'jsonapi'; | ||
|
||
|
@@ -65,20 +63,6 @@ public function getSupportedTypes($format): array | |
return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : []; | ||
} | ||
|
||
public function hasCacheableSupportsMethod(): bool | ||
{ | ||
if (method_exists(Serializer::class, 'getSupportedTypes')) { | ||
trigger_deprecation( | ||
'api-platform/core', | ||
'3.1', | ||
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', | ||
__METHOD__ | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function getSourcePointerFromViolation(ConstraintViolationInterface $violation): string | ||
{ | ||
$fieldName = $violation->getPropertyPath(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,31 +13,26 @@ | |
|
||
namespace ApiPlatform\JsonApi\Serializer; | ||
|
||
use ApiPlatform\Api\Entrypoint; | ||
use ApiPlatform\Api\IriConverterInterface as LegacyIriConverterInterface; | ||
use ApiPlatform\Api\UrlGeneratorInterface as LegacyUrlGeneratorInterface; | ||
use ApiPlatform\Documentation\Entrypoint as DocumentationEntrypoint; | ||
use ApiPlatform\Documentation\Entrypoint; | ||
use ApiPlatform\Metadata\CollectionOperationInterface; | ||
use ApiPlatform\Metadata\Exception\InvalidArgumentException; | ||
use ApiPlatform\Metadata\HttpOperation; | ||
use ApiPlatform\Metadata\IriConverterInterface; | ||
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
use ApiPlatform\Metadata\UrlGeneratorInterface; | ||
use ApiPlatform\Serializer\CacheableSupportsMethodInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
/** | ||
* Normalizes the API entrypoint. | ||
* | ||
* @author Amrouche Hamza <[email protected]> | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class EntrypointNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface | ||
final class EntrypointNormalizer implements NormalizerInterface | ||
{ | ||
public const FORMAT = 'jsonapi'; | ||
|
||
public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly IriConverterInterface|LegacyIriConverterInterface $iriConverter, private readonly UrlGeneratorInterface|LegacyUrlGeneratorInterface $urlGenerator) | ||
public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly IriConverterInterface $iriConverter, private readonly UrlGeneratorInterface $urlGenerator) | ||
{ | ||
} | ||
|
||
|
@@ -75,25 +70,11 @@ public function normalize(mixed $object, ?string $format = null, array $context | |
*/ | ||
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool | ||
{ | ||
return self::FORMAT === $format && ($data instanceof Entrypoint || $data instanceof DocumentationEntrypoint); | ||
return self::FORMAT === $format && $data instanceof Entrypoint; | ||
} | ||
|
||
public function getSupportedTypes($format): array | ||
{ | ||
return self::FORMAT === $format ? [Entrypoint::class => true, DocumentationEntrypoint::class => true] : []; | ||
} | ||
|
||
public function hasCacheableSupportsMethod(): bool | ||
{ | ||
if (method_exists(Serializer::class, 'getSupportedTypes')) { | ||
trigger_deprecation( | ||
'api-platform/core', | ||
'3.1', | ||
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', | ||
__METHOD__ | ||
); | ||
} | ||
|
||
return true; | ||
return self::FORMAT === $format ? [Entrypoint::class => true] : []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,67 +13,58 @@ | |
|
||
namespace ApiPlatform\JsonApi\Serializer; | ||
|
||
use ApiPlatform\Serializer\CacheableSupportsMethodInterface; | ||
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface; | ||
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface; | ||
use Symfony\Component\ErrorHandler\Exception\FlattenException; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
/** | ||
* Converts {@see \Exception} or {@see FlattenException} or to a JSON API error representation. | ||
* | ||
* @author Héctor Hurtarte <[email protected]> | ||
*/ | ||
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface | ||
final class ErrorNormalizer implements NormalizerInterface | ||
{ | ||
use ErrorNormalizerTrait; | ||
|
||
public const FORMAT = 'jsonapi'; | ||
public const TITLE = 'title'; | ||
private array $defaultContext = [ | ||
self::TITLE => 'An error occurred', | ||
]; | ||
|
||
public function __construct(private readonly bool $debug = false, array $defaultContext = [], private ?NormalizerInterface $itemNormalizer = null, private ?NormalizerInterface $constraintViolationListNormalizer = null) | ||
public function __construct(private ?NormalizerInterface $itemNormalizer = null) | ||
{ | ||
$this->defaultContext = array_merge($this->defaultContext, $defaultContext); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize(mixed $object, ?string $format = null, array $context = []): array | ||
{ | ||
// TODO: in api platform 4 this will be the default, note that JSON:API is close to Problem so we should use the same normalizer | ||
if ($context['rfc_7807_compliant_errors'] ?? false) { | ||
if ($object instanceof LegacyConstraintViolationListAwareExceptionInterface || $object instanceof ConstraintViolationListAwareExceptionInterface) { | ||
// TODO: return ['errors' => $this->constraintViolationListNormalizer(...)] | ||
return $this->constraintViolationListNormalizer->normalize($object->getConstraintViolationList(), $format, $context); | ||
} | ||
|
||
$jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context); | ||
$error = $jsonApiObject['data']['attributes']; | ||
$error['id'] = $jsonApiObject['data']['id']; | ||
$error['type'] = $jsonApiObject['data']['id']; | ||
|
||
return ['errors' => [$error]]; | ||
$jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context); | ||
$error = $jsonApiObject['data']['attributes']; | ||
$error['id'] = $jsonApiObject['data']['id']; | ||
if (isset($error['type'])) { | ||
$error['links'] = ['type' => $error['type']]; | ||
} | ||
|
||
$data = [ | ||
'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE], | ||
'description' => $this->getErrorMessage($object, $context, $this->debug), | ||
]; | ||
if (!isset($error['code']) && method_exists($object, 'getId')) { | ||
$error['code'] = $object->getId(); | ||
} | ||
|
||
if (null !== $errorCode = $this->getErrorCode($object)) { | ||
$data['code'] = $errorCode; | ||
if (!isset($error['violations'])) { | ||
return ['errors' => [$error]]; | ||
} | ||
|
||
if ($this->debug && null !== $trace = $object->getTrace()) { | ||
$data['trace'] = $trace; | ||
$errors = []; | ||
foreach ($error['violations'] as $violation) { | ||
$e = ['detail' => $violation['message']] + $error; | ||
if (isset($error['links']['type'])) { | ||
$type = $error['links']['type']; | ||
$e['links']['type'] = \sprintf('%s/%s', $type, $violation['propertyPath']); | ||
$e['id'] = str_replace($type, $e['links']['type'], $e['id']); | ||
} | ||
if (isset($e['code'])) { | ||
$e['code'] = \sprintf('%s/%s', $error['code'], $violation['propertyPath']); | ||
} | ||
unset($e['violations']); | ||
$errors[] = $e; | ||
} | ||
|
||
return $data; | ||
return ['errors' => $errors]; | ||
} | ||
|
||
/** | ||
|
@@ -95,18 +86,4 @@ public function getSupportedTypes($format): array | |
|
||
return []; | ||
} | ||
|
||
public function hasCacheableSupportsMethod(): bool | ||
{ | ||
if (method_exists(Serializer::class, 'getSupportedTypes')) { | ||
trigger_deprecation( | ||
'api-platform/core', | ||
'3.1', | ||
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', | ||
__METHOD__ | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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\JsonApi\Tests\Fixtures; | ||
|
||
use ApiPlatform\Metadata\Exception\ErrorCodeSerializableInterface; | ||
|
||
class ErrorCodeSerializable extends \Exception implements ErrorCodeSerializableInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getErrorCode(): string | ||
{ | ||
return '1234'; | ||
} | ||
} |
Oops, something went wrong.