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

Skip to content

Denormalizer tries to process null attributes even though they are nullable #27384

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

Closed
fpalluel opened this issue May 25, 2018 · 4 comments
Closed

Comments

@fpalluel
Copy link

fpalluel commented May 25, 2018

Symfony version(s) affected: 4

Description
Symfony tries to denormalize nested models null attributes even though they are nullable. It occurs when those models use constructor methods.

How to reproduce

class Model
{
    /**
     * @var SubModel|null
     */
    private $subModel;

    public function __construct(?SubModel $subModel)
    {
        $this->subModel = $subModel;
    }
}

class SubModel
{
    /**
     * @var DateTimeImmutable|null
     */
    private $date;

    public function __construct(?DateTimeImmutable $date)
    {
        $this->date = $date;

    }
}

When trying to deserialize a Model (with a SubModel containing a null $date), a NotNormalizableValueException is thrown, with the following message : The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.

Possible Solution
As a workaround, use setters instead of constructors.

The problem seems to lie in AbstractNormalizer, in instantiateObject method : it tries to denormalize the constructor parameter, even if its value is null.

@ogizanagi
Copy link
Contributor

Thanks for the report @fpalluel. #27389 should fix this issue. It'd be great if you could try and confirm it does :)

@fpalluel
Copy link
Author

Hello,
Just tried your fix, and it's working ;-)
Thanks for your quick answer !

ogizanagi added a commit that referenced this issue May 30, 2018
…es on nullable properties (ogizanagi)

This PR was merged into the 3.4 branch.

Discussion
----------

[Serializer] Fix serializer tries to denormalize null values on nullable properties

| Q             | A
| ------------- | ---
| Branch?       | 3.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #27384   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

Commits
-------

ca31488 [Serializer] Fix serializer tries to denormalize null values on nullable properties
@ka4kok
Copy link

ka4kok commented Jul 9, 2021

@TheThor
Copy link

TheThor commented Mar 18, 2025

Hi, I still have this happening when denormalizing an array to object. The property is not in the constructor, but is defined as private ?string and the comment also typed to string|null. I'm writing from my phone, but I can post a code example if further explanation is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants