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

Skip to content

Commit daf2605

Browse files
dunglasRomaixn
authored andcommitted
fix: don't implement deprecated CacheableSupportsMethodInterface with Symfony 6.3+ (api-platform#5696)
* fix: don't implement deprecated CacheableSupportsMethodInterface * fix: a check, and add tests * fix ApiGatewayNormalizerTest * more fixes * fix more tests * fix lowest * only trigger the deprecation for Symfony 6.3
1 parent 1939d21 commit daf2605

42 files changed

Lines changed: 521 additions & 97 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Elasticsearch/Serializer/ItemNormalizer.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
namespace ApiPlatform\Elasticsearch\Serializer;
1515

16+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1617
use Symfony\Component\Serializer\Exception\LogicException;
17-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
18+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
1819
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1920
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21+
use Symfony\Component\Serializer\Serializer;
2022
use Symfony\Component\Serializer\SerializerAwareInterface;
2123
use Symfony\Component\Serializer\SerializerInterface;
2224

@@ -39,8 +41,17 @@ public function __construct(private readonly NormalizerInterface $decorated)
3941
*/
4042
public function hasCacheableSupportsMethod(): bool
4143
{
42-
if (!$this->decorated instanceof CacheableSupportsMethodInterface) {
43-
throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', CacheableSupportsMethodInterface::class));
44+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
45+
trigger_deprecation(
46+
'api-platform/core',
47+
'3.1',
48+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
49+
__METHOD__
50+
);
51+
}
52+
53+
if (!$this->decorated instanceof BaseCacheableSupportsMethodInterface) {
54+
throw new LogicException(sprintf('The decorated normalizer must be an instance of "%s".', BaseCacheableSupportsMethodInterface::class));
4455
}
4556

4657
return $this->decorated->hasCacheableSupportsMethod();
@@ -96,11 +107,11 @@ public function getSupportedTypes($format): array
96107
if (!method_exists($this->decorated, 'getSupportedTypes')) {
97108
return [
98109
DocumentNormalizer::FORMAT => null,
99-
'*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
110+
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
100111
];
101112
}
102113

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

106117
/**

src/GraphQl/Serializer/ObjectNormalizer.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
use ApiPlatform\Api\IdentifiersExtractorInterface;
1717
use ApiPlatform\Api\IriConverterInterface;
1818
use ApiPlatform\Metadata\Util\ClassInfoTrait;
19+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1920
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
20-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
21+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
2122
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23+
use Symfony\Component\Serializer\Serializer;
2224

2325
/**
2426
* Decorates the output with GraphQL metadata when appropriate, but otherwise just
@@ -49,7 +51,7 @@ public function getSupportedTypes($format): array
4951
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
5052
if (!method_exists($this->decorated, 'getSupportedTypes')) {
5153
return [
52-
'*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
54+
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
5355
];
5456
}
5557

@@ -61,9 +63,16 @@ public function getSupportedTypes($format): array
6163
*/
6264
public function hasCacheableSupportsMethod(): bool
6365
{
64-
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
66+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
67+
trigger_deprecation(
68+
'api-platform/core',
69+
'3.1',
70+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
71+
__METHOD__
72+
);
73+
}
6574

66-
return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
75+
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
6776
}
6877

6978
/**

src/Hal/Serializer/EntrypointNormalizer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
use ApiPlatform\Metadata\CollectionOperationInterface;
2121
use ApiPlatform\Metadata\Operation;
2222
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
23+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
2424
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25+
use Symfony\Component\Serializer\Serializer;
2526

2627
/**
2728
* Normalizes the API entrypoint.
@@ -81,7 +82,14 @@ public function getSupportedTypes($format): array
8182

8283
public function hasCacheableSupportsMethod(): bool
8384
{
84-
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
85+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
86+
trigger_deprecation(
87+
'api-platform/core',
88+
'3.1',
89+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
90+
__METHOD__
91+
);
92+
}
8593

8694
return true;
8795
}

src/Hal/Serializer/ObjectNormalizer.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
namespace ApiPlatform\Hal\Serializer;
1515

1616
use ApiPlatform\Api\IriConverterInterface;
17+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1718
use Symfony\Component\Serializer\Exception\LogicException;
18-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
19+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
1920
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2021
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22+
use Symfony\Component\Serializer\Serializer;
2123

2224
/**
2325
* Decorates the output with JSON HAL metadata when appropriate, but otherwise
@@ -44,7 +46,7 @@ public function getSupportedTypes($format): array
4446
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
4547
if (!method_exists($this->decorated, 'getSupportedTypes')) {
4648
return [
47-
'*' => $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
49+
'*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
4850
];
4951
}
5052

@@ -56,9 +58,16 @@ public function getSupportedTypes($format): array
5658
*/
5759
public function hasCacheableSupportsMethod(): bool
5860
{
59-
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
61+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
62+
trigger_deprecation(
63+
'api-platform/core',
64+
'3.1',
65+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
66+
__METHOD__
67+
);
68+
}
6069

61-
return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
70+
return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
6271
}
6372

6473
/**

src/Hydra/Serializer/CollectionFiltersNormalizer.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
use ApiPlatform\Api\ResourceClassResolverInterface;
1919
use ApiPlatform\Doctrine\Orm\State\Options;
2020
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
2122
use Psr\Container\ContainerInterface;
2223
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
2324
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
24-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
25+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
2526
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
2627
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
28+
use Symfony\Component\Serializer\Serializer;
2729

2830
/**
2931
* Enhances the result of collection by adding the filters applied on collection.
@@ -54,17 +56,24 @@ public function getSupportedTypes($format): array
5456
{
5557
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
5658
if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
57-
return ['*' => $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod()];
59+
return ['*' => $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod()];
5860
}
5961

6062
return $this->collectionNormalizer->getSupportedTypes($format);
6163
}
6264

6365
public function hasCacheableSupportsMethod(): bool
6466
{
65-
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
67+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
68+
trigger_deprecation(
69+
'api-platform/core',
70+
'3.1',
71+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
72+
__METHOD__
73+
);
74+
}
6675

67-
return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
76+
return $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
6877
}
6978

7079
/**

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2727
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2828
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
29+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
2930
use Symfony\Component\PropertyInfo\Type;
3031
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
3132
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
32-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
3333
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
34+
use Symfony\Component\Serializer\Serializer;
3435

3536
/**
3637
* Creates a machine readable Hydra API documentation.
@@ -551,7 +552,14 @@ public function getSupportedTypes($format): array
551552

552553
public function hasCacheableSupportsMethod(): bool
553554
{
554-
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
555+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
556+
trigger_deprecation(
557+
'api-platform/core',
558+
'3.1',
559+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
560+
__METHOD__
561+
);
562+
}
555563

556564
return true;
557565
}

src/Hydra/Serializer/EntrypointNormalizer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
use ApiPlatform\Exception\OperationNotFoundException;
2121
use ApiPlatform\Metadata\CollectionOperationInterface;
2222
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
23+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
2424
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25+
use Symfony\Component\Serializer\Serializer;
2526

2627
/**
2728
* Normalizes the API entrypoint.
@@ -90,7 +91,14 @@ public function getSupportedTypes($format): array
9091

9192
public function hasCacheableSupportsMethod(): bool
9293
{
93-
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
94+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
95+
trigger_deprecation(
96+
'api-platform/core',
97+
'3.1',
98+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
99+
__METHOD__
100+
);
101+
}
94102

95103
return true;
96104
}

src/Hydra/Serializer/ErrorNormalizer.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
use ApiPlatform\Api\UrlGeneratorInterface;
1717
use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
18+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1819
use Symfony\Component\ErrorHandler\Exception\FlattenException;
19-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
2020
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21+
use Symfony\Component\Serializer\Serializer;
2122

2223
/**
2324
* Converts {@see \Exception} or {@see FlattenException} to a Hydra error representation.
@@ -79,7 +80,14 @@ public function getSupportedTypes($format): array
7980

8081
public function hasCacheableSupportsMethod(): bool
8182
{
82-
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
83+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
84+
trigger_deprecation(
85+
'api-platform/core',
86+
'3.1',
87+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
88+
__METHOD__
89+
);
90+
}
8391

8492
return true;
8593
}

src/Hydra/Serializer/PartialCollectionViewNormalizer.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515

1616
use ApiPlatform\Metadata\HttpOperation;
1717
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1819
use ApiPlatform\State\Pagination\PaginatorInterface;
1920
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
2021
use ApiPlatform\Util\IriHelper;
2122
use Symfony\Component\PropertyAccess\PropertyAccess;
2223
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2324
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
24-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
25+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
2526
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
2627
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
28+
use Symfony\Component\Serializer\Serializer;
2729

2830
/**
2931
* Adds a view key to the result of a paginated Hydra collection.
@@ -111,7 +113,7 @@ public function getSupportedTypes($format): array
111113
// @deprecated remove condition when support for symfony versions under 6.3 is dropped
112114
if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
113115
return [
114-
'*' => $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod(),
116+
'*' => $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod(),
115117
];
116118
}
117119

@@ -120,9 +122,16 @@ public function getSupportedTypes($format): array
120122

121123
public function hasCacheableSupportsMethod(): bool
122124
{
123-
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
125+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
126+
trigger_deprecation(
127+
'api-platform/core',
128+
'3.1',
129+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
130+
__METHOD__
131+
);
132+
}
124133

125-
return $this->collectionNormalizer instanceof CacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
134+
return $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod();
126135
}
127136

128137
/**

src/JsonApi/Serializer/ConstraintViolationListNormalizer.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
namespace ApiPlatform\JsonApi\Serializer;
1515

1616
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
17+
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1718
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
18-
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
1919
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20+
use Symfony\Component\Serializer\Serializer;
2021
use Symfony\Component\Validator\ConstraintViolationInterface;
2122
use Symfony\Component\Validator\ConstraintViolationListInterface;
2223

@@ -61,12 +62,19 @@ public function supportsNormalization(mixed $data, string $format = null, array
6162

6263
public function getSupportedTypes($format): array
6364
{
64-
return (self::FORMAT === $format) ? [ConstraintViolationListInterface::class => true] : [];
65+
return self::FORMAT === $format ? [ConstraintViolationListInterface::class => true] : [];
6566
}
6667

6768
public function hasCacheableSupportsMethod(): bool
6869
{
69-
trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
70+
if (method_exists(Serializer::class, 'getSupportedTypes')) {
71+
trigger_deprecation(
72+
'api-platform/core',
73+
'3.1',
74+
'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
75+
__METHOD__
76+
);
77+
}
7078

7179
return true;
7280
}

0 commit comments

Comments
 (0)