Skip to content

Commit 3b7df9a

Browse files
committed
bug symfony#44908 [Serializer] Fix AbstractObjectNormalizer TypeError on denormalization (JustDylan23)
This PR was squashed before being merged into the 6.0 branch. Discussion ---------- [Serializer] Fix AbstractObjectNormalizer TypeError on denormalization | Q | A | ------------- | --- | Branch? | 6.0 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#44872 | License | MIT | Doc PR | When using dependency injection to get the serializer (instead of manually instantiating it) the object normalizer that is injected into that serializer throws a value exception when doing denormalizing the following: ```php class ObjectOuter { public ObjectInner $inner; } class ObjectInner { public $foo; } public function testDenormalizeRecursiveWithObjectAttributeWithStringValue() { $extractor = new ReflectionExtractor(); $normalizer = new ObjectNormalizer(null, null, null, $extractor); $serializer = new Serializer([$normalizer]); $obj = $serializer->denormalize(['inner' => 'foo'], ObjectOuter::class); $this->assertInstanceOf(ObjectInner::class, $obj->getInner()); } ``` This throws ```php TypeError: Symfony\Component\Serializer\Normalizer\AbstractNormalizer::prepareForDenormalization(): Argument #1 ($data) must be of type object|array|null, string given, called in /var/www/symfony/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php on line 368 at vendor/symfony/serializer/Normalizer/AbstractNormalizer.php:299 at Symfony\Component\Serializer\Normalizer\AbstractNormalizer->prepareForDenormalization('test string') (vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:368) at Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->denormalize('test string', 'App\\Entity\\User', null, array('cache_key' => 'c93a6d4efa206ea58a62cc6b7fab8dfb', 'deserialization_path' => 'author')) (vendor/symfony/serializer/Serializer.php:238) at Symfony\Component\Serializer\Serializer->denormalize('test string', 'App\\Entity\\User', null, array('cache_key' => 'c93a6d4efa206ea58a62cc6b7fab8dfb', 'deserialization_path' => 'author')) (vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:559) at Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->validateAndDenormalize(array(object(Type)), 'App\\Entity\\Blog', 'author', 'test string', null, array('cache_key' => '44db5a926a1544b1a8585af40107ca3a', 'deserialization_path' => 'author')) (vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:401) at Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->denormalize(array('author' => 'test string'), 'App\\Entity\\Blog', null, array('cache_key' => '44db5a926a1544b1a8585af40107ca3a')) (vendor/symfony/serializer/Serializer.php:238) at Symfony\Component\Serializer\Serializer->denormalize(array('author' => 'test string'), 'App\\Entity\\Blog') (src/Controller/BugReproductionController.php:18) at App\Controller\BugReproductionController->test(object(Serializer)) (vendor/symfony/http-kernel/HttpKernel.php:152) at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1) (vendor/symfony/http-kernel/HttpKernel.php:74) at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true) (vendor/symfony/http-kernel/Kernel.php:202) at Symfony\Component\HttpKernel\Kernel->handle(object(Request)) (vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:35) at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run() (vendor/autoload_runtime.php:29) at require_once('/var/www/symfony/vendor/autoload_runtime.php') (public/index.php:5) ``` Refer to: symfony#44881 for the description. Was in the middle of changing the base branch and accidentally pushed when the branch was deleted. `@fancyweb` I implemented the requested changes Commits ------- 89092ea [Serializer] Fix AbstractObjectNormalizer TypeError on denormalization
2 parents 1a02d0c + 89092ea commit 3b7df9a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ protected function isAllowedAttribute(object|string $classOrObject, string $attr
296296
* Normalizes the given data to an array. It's particularly useful during
297297
* the denormalization process.
298298
*/
299-
protected function prepareForDenormalization(object|array|null $data): array
299+
protected function prepareForDenormalization(mixed $data): array
300300
{
301301
return (array) $data;
302302
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
17+
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1718
use Symfony\Component\PropertyInfo\Type;
1819
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
1920
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
@@ -387,6 +388,17 @@ public function testNormalizeEmptyObject()
387388
$normalizedData = $normalizer->normalize(new EmptyDummy(), 'any', ['preserve_empty_objects' => true]);
388389
$this->assertEquals(new \ArrayObject(), $normalizedData);
389390
}
391+
392+
public function testDenormalizeRecursiveWithObjectAttributeWithStringValue()
393+
{
394+
$extractor = new ReflectionExtractor();
395+
$normalizer = new ObjectNormalizer(null, null, null, $extractor);
396+
$serializer = new Serializer([$normalizer]);
397+
398+
$obj = $serializer->denormalize(['inner' => 'foo'], ObjectOuter::class);
399+
400+
$this->assertInstanceOf(ObjectInner::class, $obj->getInner());
401+
}
390402
}
391403

392404
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer

0 commit comments

Comments
 (0)