8000 [Serializer] Allow filtering "object" when using "getSupportedTypes()" · symfony/symfony@6984dce · GitHub
[go: up one dir, main page]

Skip to content

Commit 6984dce

Browse files
[Serializer] Allow filtering "object" when using "getSupportedTypes()"
1 parent ecd57a6 commit 6984dce

File tree

11 files changed

+42
-41
lines changed

11 files changed

+42
-41
lines changed

src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ public function setDenormalizer(DenormalizerInterface $denormalizer): void
3838

3939
public function getSupportedTypes(?string $format): array
4040
{
41-
// @deprecated remove condition in 7.0
42-
if (!method_exists($this->denormalizer, 'getSupportedTypes')) {
43-
return ['*' => $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod()];
44-
}
45-
46-
return $this->denormalizer->getSupportedTypes($format);
41+
return ['object' => null, '*' => false];
4742
}
4843

4944
/**

src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
5151
/**
5252
* Checks whether the given class is supported for denormalization by this normalizer.
5353
*
54-
* Since Symfony 6.3, this method will only be called if the type is
55-
* included in the supported types returned by getSupportedTypes().
56-
*
57-
* @see getSupportedTypes()
58-
*
5954
* @param mixed $data Data to denormalize from
6055
* @param string $type The class to which the data should be denormalized
6156
* @param string|null $format The format being deserialized from
@@ -72,12 +67,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
7267
* returned as keys, and each type should be mapped to a boolean indicating
7368
* if the result of supportsDenormalization() can be cached or not
7469
* (a result cannot be cached when it depends on the context or on the data.)
70+
* A null value means that the denormalizer does not support the corresponding
71+
* type.
7572
*
76-
* The special type '*' can be used to indicate that the denormalizer might
77-
* support any types. A null value means that the denormalizer does not support
78-
* the corresponding type.
73+
* Use type "object" to match any classes or interfaces,
74+
* and type "*" to match any types.
7975
*
80-
* @return array<class-string|'*'|string, bool|null>
76+
* @return array<class-string|'*'|'object'|string, bool|null>
8177
*/
8278
/* public function getSupportedTypes(?string $format): array; */
8379
}

src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
3838

3939
public function getSupportedTypes(?string $format): array
4040
{
41-
return ['*' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
41+
return ['object' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
4242
}
4343

4444
/**

src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ public function normalize(mixed $object, string $format = null, array $context =
4343
/**
4444
* Checks whether the given class is supported for normalization by this normalizer.
4545
*
46-
* Since Symfony 6.3, this method will only be called if the $data type is
47-
* included in the supported types returned by getSupportedTypes().
48-
*
49-
* @see getSupportedTypes()
50-
*
5146
* @param mixed $data Data to normalize
5247
* @param string|null $format The format being (de-)serialized from or into
5348
* @param array $context Context options for the normalizer
@@ -63,12 +58,13 @@ public function supportsNormalization(mixed $data, string $format = null /* , ar
6358
* returned as keys, and each type should be mapped to a boolean indicating
6459
* if the result of supportsNormalization() can be cached or not
6560
* (a result cannot be cached when it depends on the context or on the data.)
61+
* A null value means that the normalizer does not support the corresponding
62+
* type.
6663
*
67-
* The special type '*' can be used to indicate that the normalizer might
68-
* support any types. A null value means that the normalizer does not support
69-
* the corresponding type.
64+
* Use type "object" to match any classes or interfaces,
65+
* and type "*" to match any types.
7066
*
71-
* @return array<class-string|'*'|string, bool|null>
67+
* @return array<class-string|'*'|'object'|string, bool|null>
7268
*/
7369
/* public function getSupportedTypes(?string $format): array; */
7470
}

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
4949

5050
public function getSupportedTypes(?string $format): array
5151
{
52-
return ['*' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
52+
return ['object' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
5353
}
5454

5555
/**

src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
5656

5757
public function getSupportedTypes(?string $format): array
5858
{
59-
return ['*' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
59+
return ['object' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
6060
}
6161

6262
/**

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
251251
*/
252252
private function getNormalizer(mixed $data, ?string $format, array $context): ?NormalizerInterface
253253
{
254-
$type = \is_object($data) ? $data::class : 'native-'.\gettype($data);
254+
if (\is_object($data)) {
255+
$type = $data::class;
256+
$genericType = 'object';
257+
} else {
258+
$type = 'native-'.\gettype($data);
259+
$genericType = '*';
260+
}
255261

256262
if (!isset($this->normalizerCache[$format][$type])) {
257263
$this->normalizerCache[$format][$type] = [];
@@ -277,20 +283,22 @@ private function getNormalizer(mixed $data, ?string $format, array $context): ?N
277283
$supportedTypes = $normalizer->getSupportedTypes($format);
278284

279285
foreach ($supportedTypes as $supportedType => $isCacheable) {
280-
if ('*' === $supportedType || $type !== $supportedType && !is_subclass_of($type, $supportedType, true)) {
286+
if (\in_array($supportedType, ['*', 'object'], true)
287+
|| $type !== $supportedType && ('object' !== $genericType || !is_subclass_of($type, $supportedType))
288+
) {
281289
continue;
282290
}
283291

284292
if (null === $isCacheable) {
285-
unset($supportedTypes['*']);
293+
unset($supportedTypes['*'], $supportedTypes['object']);
286294
} elseif ($this->normalizerCache[$format][$type][$k] = $isCacheable && $normalizer->supportsNormalization($data, $format, $context)) {
287295
break 2;
288296
}
289297

290298
break;
291299
}
292300

293-
if (null === $isCacheable = $supportedTypes['*'] ?? null) {
301+
if (null === $isCacheable = $supportedTypes[\array_key_exists($genericType, $supportedTypes) ? $genericType : '*'] ?? null) {
294302
continue;
295303
}
296304

@@ -322,6 +330,7 @@ private function getDenormalizer(mixed $data, string $class, ?string $format, ar
322330
{
323331
if (!isset($this->denormalizerCache[$format][$class])) {
324332
$this->denormalizerCache[$format][$class] = [];
333+
$genericType = class_exists($class) || interface_exists($class, false) ? 'object' : '*';
325334

326335
foreach ($this->normalizers as $k => $normalizer) {
327336
if (!$normalizer instanceof DenormalizerInterface) {
@@ -344,20 +353,22 @@ private function getDenormalizer(mixed $data, string $class, ?string $format, ar
344353
$supportedTypes = $normalizer->getSupportedTypes($format);
345354

346355
foreach ($supportedTypes as $supportedType => $isCacheable) {
347-
if ('*' === $supportedType || $class !== $supportedType && !is_subclass_of($class, $supportedType, true)) {
356+
if (\in_array($supportedType, ['*', 'object'], true)
357+
|| $class !== $supportedType && ('object' !== $genericType || !is_subclass_of($class, $supportedType))
358+
) {
348359
continue;
349360
}
350361

351362
if (null === $isCacheable) {
352-
unset($supportedTypes['*']);
363+
unset($supportedTypes['*'], $supportedTypes['object']);
353364
} elseif ($this->denormalizerCache[$format][$class][$k] = $isCacheable && $normalizer->supportsDenormalization(null, $class, $format, $context)) {
354365
break 2;
355366
}
356367

357368
break;
358369
}
359370

360-
if (null === $isCacheable = $supportedTypes['*'] ?? null) {
371+
if (null === $isCacheable = $supportedTypes[\array_key_exists($genericType, $supportedTypes) ? $genericType : '*'] ?? null) {
361372
continue;
362373
}
363374

src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/GroupDummy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;
1313

1414
use Symfony\Component\Serializer\Annotation\Groups;
15-
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface;
1615
use Symfony\Component\Serializer\Tests\Fixtures\ChildOfGroupsAnnotationDummy;
16+
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface;
1717

1818
/**
1919
* @author Kévin Dunglas <dunglas@gmail.com>

src/Symfony/Component/Serializer/Tests/Fixtures/DummyObjectWithEnumConstructor.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
<?php
22

3-
namespace Symfony\Component\Serializer\Tests\Fixtures;
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
411

5-
use Symfony\Component\Serializer\Tests\Fixtures\StringBackedEnumDummy;
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
613

714
class DummyObjectWithEnumConstructor
815
{

src/Symfony/Component/Serializer/Tests/Fixtures/Php74Full.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ final class Php74Full
3535
public $anotherCollection;
3636
}
3737

38-
3938
final class Php74FullWithConstructor
4039
{
4140
public function __construct($constructorArgument)

src/Symfony/Component/Serializer/Tests/SerializerTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1818
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1919
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
20-
use Symfony\Component\Serializer\Encoder\DecoderInterface;
21-
use Symfony\Component\Serializer\Encoder\EncoderInterface;
2220
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2321
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
2422
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
@@ -49,7 +47,6 @@
4947
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
5048
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
5149
use Symfony\Component\Serializer\Serializer;
52-
use Symfony\Component\Serializer\SerializerInterface;
5350
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy;
5451
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummyFirstChild;
5552
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummySecondChild;

0 commit comments

Comments
 (0)
0