-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #51369 [Serializer] Fix deserializing object collection propertie…
…s (X-Coder264) This PR was merged into the 6.3 branch. Discussion ---------- [Serializer] Fix deserializing object collection properties | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #51261 | License | MIT | Doc PR | - On Symfony <= 6.2 serializer versions the `supportsDenormalization` was always called in `\Symfony\Component\Serializer\Serializer::getDenormalizer`. When the `getSupportedTypes` logic was introduced for Symfony 6.3 serializer some `if` statement conditions were introduced and if they are `true` the `supportsDenormalization` logic is not called at all anymore. Those conditions currently prevent a legit use-case that worked prior to 6.3 from calling the `supportsDenormalization` method - the case being when the denormalizer is supposed to denormalize a collection/array of objects. This use-case still works even on the 6.3 serializer as long as the denormalizer does not implement the new `getSupportedTypes` method, but that is triggering a deprecation. This PR aims to fix that so that denormalizing an array of objects still works even when using the new `getSupportedTypes` method in a denormalizer. cc `@tucksaun` `@nicolas`-grekas Commits ------- 32dc13432f [Serializer] Fix deserializing object collection properties
- Loading branch information
Showing
6 changed files
with
132 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
interface FooDummyInterface | ||
{ | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
final class FooImplementationDummy implements FooDummyInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $name; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
|
||
final class FooInterfaceDummyDenormalizer implements DenormalizerInterface | ||
{ | ||
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): array | ||
{ | ||
$result = []; | ||
foreach ($data as $foo) { | ||
$fooDummy = new FooImplementationDummy(); | ||
$fooDummy->name = $foo['name']; | ||
$result[] = $fooDummy; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool | ||
{ | ||
if (str_ends_with($type, '[]')) { | ||
$className = substr($type, 0, -2); | ||
$classImplements = class_implements($className); | ||
\assert(\is_array($classImplements)); | ||
|
||
return class_exists($className) && \in_array(FooDummyInterface::class, $classImplements, true); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return array<string, bool> | ||
*/ | ||
public function getSupportedTypes(?string $format): array | ||
{ | ||
return [FooDummyInterface::class.'[]' => false]; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Fixtures; | ||
|
||
final class ObjectCollectionPropertyDummy | ||
{ | ||
/** | ||
* @var FooImplementationDummy[] | ||
*/ | ||
public $foo; | ||
|
||
public function getFoo(): array | ||
{ | ||
return $this->foo; | ||
} | ||
} |
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