Skip to content
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

Nullable properties are not initialized on deserialization #1252

Closed
simPod opened this issue Oct 1, 2020 · 4 comments
Closed

Nullable properties are not initialized on deserialization #1252

simPod opened this issue Oct 1, 2020 · 4 comments

Comments

@simPod
Copy link
Contributor

simPod commented Oct 1, 2020

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

Steps required to reproduce the problem

  1. Serialize this
<?php

declare(strict_types=1);

final class A
{
    public string $a;

    public ?string $b;

    public function __construct(string $a, ?string $b)
    {
        $this->a = $a;
        $this->b = $b;
    }
}

serialize(new A("1",null));
  1. The json is {"a": "1"}
  2. Deserialize it
  3. Access $a->b

Expected Result

No error, $a->b gives null

Actual Result

Typed property A::$b must not be accessed before initialization

@goetas
Copy link
Collaborator

goetas commented Oct 1, 2020

Hi. I think that you should use the $context->serializeNulls() feature in that case. Did you try it?

@simPod
Copy link
Contributor Author

simPod commented Oct 1, 2020

Ah right, forgot about that. Works!

BTW shouldn't it be default on PHP 7.4+?

@goetas
Copy link
Collaborator

goetas commented Oct 3, 2020

I was thinking to solve it with #1081 but never had time to continue the work on it.

@simPod
Copy link
Contributor Author

simPod commented Oct 6, 2020

Let's continue there

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

No branches or pull requests

2 participants