-
-
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
16 changed files
with
15 additions
and
526 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
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 |
---|---|---|
|
@@ -20,17 +20,15 @@ | |
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'; | ||
|
||
|
@@ -79,18 +77,4 @@ public function getSupportedTypes($format): array | |
{ | ||
return self::FORMAT === $format ? [Entrypoint::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; | ||
} | ||
} |
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,33 @@ | |
|
||
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]]; | ||
} | ||
|
||
$data = [ | ||
'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE], | ||
'description' => $this->getErrorMessage($object, $context, $this->debug), | ||
]; | ||
|
||
if (null !== $errorCode = $this->getErrorCode($object)) { | ||
$data['code'] = $errorCode; | ||
} | ||
$jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context); | ||
$error = $jsonApiObject['data']['attributes']; | ||
$error['id'] = $jsonApiObject['data']['id']; | ||
$error['type'] = $jsonApiObject['data']['id']; | ||
|
||
if ($this->debug && null !== $trace = $object->getTrace()) { | ||
$data['trace'] = $trace; | ||
} | ||
|
||
return $data; | ||
return ['errors' => [$error]]; | ||
} | ||
|
||
/** | ||
|
@@ -95,18 +61,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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -26,7 +26,6 @@ | |
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
/** | ||
* @author Amrouche Hamza <[email protected]> | ||
|
@@ -35,9 +34,6 @@ class CollectionNormalizerTest extends TestCase | |
{ | ||
use ProphecyTrait; | ||
|
||
/** | ||
* @group legacy | ||
*/ | ||
public function testSupportsNormalize(): void | ||
{ | ||
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class); | ||
|
@@ -56,10 +52,6 @@ public function testSupportsNormalize(): void | |
'native-array' => true, | ||
'\Traversable' => true, | ||
], $normalizer->getSupportedTypes($normalizer::FORMAT)); | ||
|
||
if (!method_exists(Serializer::class, 'getSupportedTypes')) { | ||
$this->assertTrue($normalizer->hasCacheableSupportsMethod()); | ||
} | ||
} | ||
|
||
public function testNormalizePaginator(): void | ||
|
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 |
---|---|---|
|
@@ -32,7 +32,6 @@ | |
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
/** | ||
* @author Amrouche Hamza <[email protected]> | ||
|
@@ -57,10 +56,6 @@ public function testSupportNormalization(): void | |
$this->assertFalse($normalizer->supportsNormalization(new \stdClass(), EntrypointNormalizer::FORMAT)); | ||
$this->assertEmpty($normalizer->getSupportedTypes('json')); | ||
$this->assertSame([Entrypoint::class => true], $normalizer->getSupportedTypes($normalizer::FORMAT)); | ||
|
||
if (!method_exists(Serializer::class, 'getSupportedTypes')) { | ||
$this->assertTrue($normalizer->hasCacheableSupportsMethod()); | ||
} | ||
} | ||
|
||
public function testNormalize(): void | ||
|
Oops, something went wrong.