Skip to content

[Serializer] Fix AbstractObjectNormalizer TypeError on denormalization #44908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2022
Merged

[Serializer] Fix AbstractObjectNormalizer TypeError on denormalization #44908

merged 1 commit into from
Jan 12, 2022

Conversation

JustDylan23
Copy link
Contributor

@JustDylan23 JustDylan23 commented Jan 4, 2022

Q A
Branch? 6.0
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #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:

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

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: #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

@carsonbot
Copy link

Hey!

I think @Th3Mouk has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

Copy link
Contributor

@nikophil nikophil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix 👍

@fancyweb
Copy link
Contributor

fancyweb commented Jan 7, 2022

It would still makes more sense to me if the test was in AbstractObjectNormalizerTest instead of AbstractNormalizerTest 🙏

@fancyweb fancyweb changed the title [Serializer] Fix AbstractObjectNormalizer TypeError on denormalization v2 [Serializer] Fix AbstractObjectNormalizer TypeError on denormalization Jan 7, 2022
@JustDylan23
Copy link
Contributor Author

@fancyweb

It would still makes more sense to me if the test was in AbstractObjectNormalizerTest instead of AbstractNormalizerTest 🙏

I moved it 👍

@JustDylan23
Copy link
Contributor Author

JustDylan23 commented Jan 7, 2022

are the tests that are failing now something on my end? I could rebase on 6.0 if desired

@fancyweb
Copy link
Contributor

Thanks. The tests look good to me.

@fancyweb fancyweb modified the milestones: 6.1, 6.0 Jan 12, 2022
@fancyweb fancyweb added the Bug label Jan 12, 2022
@fancyweb
Copy link
Contributor

Thank you @JustDylan23.

@fancyweb fancyweb merged commit 3b7df9a into symfony:6.0 Jan 12, 2022
@fabpot fabpot mentioned this pull request Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[serializer] Code regression by adding type hints
5 participants