Skip to content

Commit

Permalink
Merge branch 'sf/remove-flag'
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Jul 19, 2024
2 parents 009739c + 8cdea99 commit 68038c4
Show file tree
Hide file tree
Showing 16 changed files with 15 additions and 526 deletions.
1 change: 0 additions & 1 deletion Serializer/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace ApiPlatform\JsonApi\Serializer;

use ApiPlatform\JsonApi\Util\IriHelper;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\Util\IriHelper;
Expand Down
18 changes: 1 addition & 17 deletions Serializer/ConstraintViolationListNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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';

Expand Down Expand Up @@ -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();
Expand Down
18 changes: 1 addition & 17 deletions Serializer/EntrypointNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
}
62 changes: 7 additions & 55 deletions Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
}

/**
Expand All @@ -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;
}
}
57 changes: 0 additions & 57 deletions Serializer/ErrorNormalizerTrait.php

This file was deleted.

26 changes: 1 addition & 25 deletions Serializer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\Util\ClassInfoTrait;
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;

/**
* Decorates the output with JSON API metadata when appropriate, but otherwise
* just passes through to the decorated normalizer.
*/
final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
final class ObjectNormalizer implements NormalizerInterface
{
use ClassInfoTrait;

Expand All @@ -46,30 +43,9 @@ public function supportsNormalization(mixed $data, ?string $format = null, array

public function getSupportedTypes($format): array
{
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
if (!method_exists($this->decorated, 'getSupportedTypes')) {
return [
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
];
}

return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
}

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 $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
}

/**
* {@inheritdoc}
*/
Expand Down
1 change: 0 additions & 1 deletion Tests/JsonSchema/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ protected function setUp(): void
$definitionNameFactory = new DefinitionNameFactory(['jsonapi' => true]);

$baseSchemaFactory = new BaseSchemaFactory(
typeFactory: null,
resourceMetadataFactory: $resourceMetadataFactory->reveal(),
propertyNameCollectionFactory: $propertyNameCollectionFactory->reveal(),
propertyMetadataFactory: $propertyMetadataFactory->reveal(),
Expand Down
8 changes: 0 additions & 8 deletions Tests/Serializer/CollectionNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
Expand All @@ -35,9 +34,6 @@ class CollectionNormalizerTest extends TestCase
{
use ProphecyTrait;

/**
* @group legacy
*/
public function testSupportsNormalize(): void
{
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
Expand All @@ -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
Expand Down
8 changes: 0 additions & 8 deletions Tests/Serializer/ConstraintViolationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
Expand All @@ -34,9 +33,6 @@ class ConstraintViolationNormalizerTest extends TestCase
{
use ProphecyTrait;

/**
* @group legacy
*/
public function testSupportNormalization(): void
{
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
Expand All @@ -49,10 +45,6 @@ public function testSupportNormalization(): void
$this->assertFalse($normalizer->supportsNormalization(new \stdClass(), ConstraintViolationListNormalizer::FORMAT));
$this->assertEmpty($normalizer->getSupportedTypes('json'));
$this->assertSame([ConstraintViolationListInterface::class => true], $normalizer->getSupportedTypes($normalizer::FORMAT));

if (!method_exists(Serializer::class, 'getSupportedTypes')) {
$this->assertTrue($normalizer->hasCacheableSupportsMethod());
}
}

public function testNormalize(): void
Expand Down
5 changes: 0 additions & 5 deletions Tests/Serializer/EntrypointNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]>
Expand All @@ -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
Expand Down
Loading

0 comments on commit 68038c4

Please sign in to comment.