Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: a check, and add tests
  • Loading branch information
dunglas committed Jul 24, 2023
commit 5ae5f045555736fbd9b363d3ee38ba2a0d2a1be7
11 changes: 7 additions & 4 deletions src/Elasticsearch/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
Expand All @@ -39,8 +40,10 @@ public function __construct(private readonly NormalizerInterface $decorated)
*/
public function hasCacheableSupportsMethod(): bool
{
if (!$this->decorated instanceof CacheableSupportsMethodInterface) {
throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', CacheableSupportsMethodInterface::class));
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
Comment thread
dunglas marked this conversation as resolved.
Outdated

if (!$this->decorated instanceof BaseCacheableSupportsMethodInterface) {
throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', BaseCacheableSupportsMethodInterface::class));
}

return $this->decorated->hasCacheableSupportsMethod();
Expand Down Expand Up @@ -96,11 +99,11 @@ public function getSupportedTypes($format): array
if (!method_exists($this->decorated, 'getSupportedTypes')) {
return [
DocumentNormalizer::FORMAT => null,
'*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
];
}

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

/**
Expand Down
5 changes: 3 additions & 2 deletions src/GraphQl/Serializer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\Util\ClassInfoTrait;
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
Expand Down Expand Up @@ -49,7 +50,7 @@ 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 CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
];
}

Expand All @@ -63,7 +64,7 @@ public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Hal/Serializer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

Expand Down Expand Up @@ -44,7 +45,7 @@ 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 CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
];
}

Expand All @@ -58,7 +59,7 @@ public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Hydra/Serializer/CollectionFiltersNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Psr\Container\ContainerInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

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

return $this->collectionNormalizer->getSupportedTypes($format);
Expand All @@ -64,7 +65,7 @@ public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
return $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Hydra/Serializer/PartialCollectionViewNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

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

Expand All @@ -122,7 +123,7 @@ public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
return $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function supportsNormalization(mixed $data, string $format = null, array

public function getSupportedTypes($format): array
{
return (self::FORMAT === $format) ? [ConstraintViolationListInterface::class => true] : [];
return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
}

public function hasCacheableSupportsMethod(): bool
Expand Down
5 changes: 3 additions & 2 deletions src/JsonApi/Serializer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Util\ClassInfoTrait;
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
Expand Down Expand Up @@ -47,7 +48,7 @@ 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 CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
];
}

Expand All @@ -58,7 +59,7 @@ public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/JsonLd/Serializer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\JsonLd\AnonymousContextBuilderInterface;
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
Expand Down Expand Up @@ -46,7 +47,7 @@ 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 CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
];
}

Expand All @@ -57,7 +58,7 @@ public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/OpenApi/Serializer/ApiGatewayNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace ApiPlatform\OpenApi\Serializer;

use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

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

return $this->documentationNormalizer->getSupportedTypes($format);
Expand All @@ -137,7 +137,7 @@ public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return $this->documentationNormalizer instanceof CacheableSupportsMethodInterface && $this->documentationNormalizer->hasCacheableSupportsMethod();
return $this->documentationNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->documentationNormalizer->hasCacheableSupportsMethod();
}

private function isLocalRef(string $ref): bool
Expand Down
46 changes: 46 additions & 0 deletions src/OpenApi/Serializer/CacheableSupportsMethodInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\OpenApi\Serializer;

use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Serializer;

if (method_exists(Serializer::class, 'getSupportedTypes')) {
/**
* Backward compatibility layer for getSupportedTypes().
*
* @internal
*
* @author Kévin Dunglas <[email protected]>
*
* @todo remove this interface when dropping support for Serializer < 6.3
*/
interface CacheableSupportsMethodInterface
{
public function getSupportedTypes(?string $format): array;
}
} else {
/**
* Backward compatibility layer for NormalizerInterface::getSupportedTypes().
*
* @internal
*
* @author Kévin Dunglas <[email protected]>
*
* @todo remove this interface when dropping support for Serializer < 6.3
*/
interface CacheableSupportsMethodInterface extends BaseCacheableSupportsMethodInterface
{
}
}
1 change: 0 additions & 1 deletion src/OpenApi/Serializer/OpenApiNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use ApiPlatform\OpenApi\Model\Paths;
use ApiPlatform\OpenApi\OpenApi;
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenApi/Tests/Serializer/ApiGatewayNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ final class ApiGatewayNormalizerTest extends TestCase
public function testSupportsNormalization(): void
{
$normalizerProphecy = $this->prophesize(NormalizerInterface::class);
$normalizerProphecy->willImplement(CacheableSupportsMethodInterface::class);
$normalizerProphecy->supportsNormalization(OpenApiNormalizer::FORMAT, OpenApi::class)->willReturn(true);
if (!method_exists(Serializer::class, 'getSupportedTypes')) {
$normalizerProphecy->willImplement(CacheableSupportsMethodInterface::class);
$normalizerProphecy->hasCacheableSupportsMethod()->willReturn(true);
}

Expand Down
4 changes: 1 addition & 3 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ public function supportsNormalization(mixed $data, string $format = null, array

public function getSupportedTypes(?string $format): array
{
return [
'*' => true,
];
return ['*' => true];
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Serializer/CacheableSupportsMethodInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ public function getSupportedTypes(?string $format): array;
*/
interface CacheableSupportsMethodInterface extends BaseCacheableSupportsMethodInterface
{
public function getSupportedTypes(?string $format): array;
}
}
12 changes: 12 additions & 0 deletions tests/Elasticsearch/Serializer/ItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public function testSetSerializer(): void
*/
public function testHasCacheableSupportsMethodWithDecoratedNormalizerNotAnInstanceOfCacheableSupportsMethodInterface(): void
{
if (method_exists(Serializer::class, 'getSupportedTypes')) {
$this->markTestSkipped('Symfony Serializer >= 6.3');
}

$this->expectException(LogicException::class);
$this->expectExceptionMessage(sprintf('The decorated normalizer must be an instance of "%s".', CacheableSupportsMethodInterface::class));

Expand Down Expand Up @@ -140,4 +144,12 @@ public function testSetSerializerWithDecoratedNormalizerNotAnInstanceOfSerialize

(new ItemNormalizer($this->prophesize(NormalizerInterface::class)->reveal()))->setSerializer($this->prophesize(SerializerInterface::class)->reveal());
}

public function testGetSupportedTypes(): void
{
$this->normalizerProphecy->getSupportedTypes(Argument::any())->willReturn(['*' => true]);

$this->assertEmpty($this->itemNormalizer->getSupportedTypes('json'));
$this->assertSame(['*' => true], $this->itemNormalizer->getSupportedTypes($this->itemNormalizer::FORMAT));
}
}
6 changes: 6 additions & 0 deletions tests/Hal/Serializer/CollectionNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public function testSupportsNormalize(): void
$this->assertFalse($normalizer->supportsNormalization([], 'xml', ['resource_class' => 'Foo']));
$this->assertFalse($normalizer->supportsNormalization(new \ArrayObject(), 'xml', ['resource_class' => 'Foo']));

$this->assertEmpty($normalizer->getSupportedTypes('json'));
$this->assertSame([
'native-array' => true,
'\Traversable' => true,
], $normalizer->getSupportedTypes($normalizer::FORMAT));

if (!method_exists(Serializer::class, 'getSupportedTypes')) {
$this->assertTrue($normalizer->hasCacheableSupportsMethod());
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Hal/Serializer/EntrypointNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function testSupportNormalization(): void
$this->assertFalse($normalizer->supportsNormalization($entrypoint, 'json'));
$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());
}
Expand Down
6 changes: 4 additions & 2 deletions tests/Hal/Serializer/ItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ public function testSupportsNormalization(): void
$nameConverter->reveal()
);

$this->assertTrue($normalizer->supportsNormalization($dummy, 'jsonhal'));
$this->assertTrue($normalizer->supportsNormalization($dummy, $normalizer::FORMAT));
$this->assertFalse($normalizer->supportsNormalization($dummy, 'xml'));
$this->assertFalse($normalizer->supportsNormalization($std, 'jsonhal'));
$this->assertFalse($normalizer->supportsNormalization($std, $normalizer::FORMAT));
$this->assertEmpty($normalizer->getSupportedTypes('xml'));
$this->assertSame(['*' => true], $normalizer->getSupportedTypes($normalizer::FORMAT));

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