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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions src/Symfony/Component/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,12 @@ private function getDenormalizer(mixed $data, string $class, ?string $format, ar

$supportedTypes = $normalizer->getSupportedTypes($format);

$doesClassRepresentCollection = str_ends_with($class, '[]');

foreach ($supportedTypes as $supportedType => $isCacheable) {
if (\in_array($supportedType, ['*', 'object'], true)
|| $class !== $supportedType && ('object' !== $genericType || !is_subclass_of($class, $supportedType))
&& !($doesClassRepresentCollection && str_ends_with($supportedType, '[]') && is_subclass_of(strstr($class, '[]', true), strstr($supportedType, '[]', true)))
) {
continue;
}
Expand Down
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 <[email protected]>
*
* 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
{
}
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 <[email protected]>
*
* 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;
}
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 <[email protected]>
*
* 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];
}
}
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 <[email protected]>
*
* 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;
}
}
18 changes: 18 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty;
use Symfony\Component\Serializer\Tests\Fixtures\FalseBuiltInDummy;
use Symfony\Component\Serializer\Tests\Fixtures\FooImplementationDummy;
use Symfony\Component\Serializer\Tests\Fixtures\FooInterfaceDummyDenormalizer;
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
use Symfony\Component\Serializer\Tests\Fixtures\ObjectCollectionPropertyDummy;
use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
use Symfony\Component\Serializer\Tests\Fixtures\Php80WithPromotedTypedConstructor;
use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
Expand Down Expand Up @@ -711,6 +714,21 @@ public function testDeserializeInconsistentScalarArray()
$serializer->deserialize('["42"]', 'int[]', 'json');
}

public function testDeserializeOnObjectWithObjectCollectionProperty()
{
$serializer = new Serializer([new FooInterfaceDummyDenormalizer(), new ObjectNormalizer(null, null, null, new PhpDocExtractor())], [new JsonEncoder()]);

$obj = $serializer->deserialize('{"foo":[{"name":"bar"}]}', ObjectCollectionPropertyDummy::class, 'json');
$this->assertInstanceOf(ObjectCollectionPropertyDummy::class, $obj);

$fooDummyObjects = $obj->getFoo();
$this->assertCount(1, $fooDummyObjects);

$fooDummyObject = $fooDummyObjects[0];
$this->assertInstanceOf(FooImplementationDummy::class, $fooDummyObject);
$this->assertSame('bar', $fooDummyObject->name);
}

public function testDeserializeWrappedScalar()
{
$serializer = new Serializer([new UnwrappingDenormalizer()], ['json' => new JsonEncoder()]);
Expand Down