8000 [Serializer] deserialize as a null when inner object cannot be created and type hint allows null by kbkk · Pull Request #26140 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] deserialize as a null when inner object cannot be created and type hint allows null #26140

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

Conversation

kbkk
Copy link
@kbkk kbkk commented Feb 11, 2018
Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
class ObjectConstructorDummy
{
    protected $foo;
    public $bar;
    private $baz;

    public function __construct($foo, $bar, $baz)
    {
        $this->foo = $foo;
        $this->bar = $bar;
        $this->baz = $baz;
    }
}

class DummyWithNullableConstructorObject
{
    private $id;
    private $inner;

    public function __construct($id, ?ObjectConstructorDummy $inner)
    {
        $this->id = $id;
        $this->inner = $inner;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getInner()
    {
        return $this->inner;
    }
}

Trying to deserialize to DummyWithNullableConstructorObject with the following data currently fails:

< 8000 div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[ 'id' => 10, 'inner' => null ]">
[
    'id' => 10,
    'inner' => null
]

With this PR DummyWithNullableConstructorObject would be constructed with null passed as $inner because of the type hint.

@kbkk kbkk changed the title deserialize as a null when inner object cannot be created and type hi… [Serializer] deserialize as a null when inner object cannot be created Feb 11, 2018
@nicolas-grekas nicolas-grekas changed the title [Serializer] deserialize as a null when inner object cannot be created [Serializer] deserialize as a null when inner object cannot be created and type hint allows null Feb 11, 2018
@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Feb 12, 2018
Copy link
Member
@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

This can even be merged as a bugfix to me.

@fabpot
Copy link
Member
fabpot commented Feb 19, 2018

Looks like a bug fix to me as well. Which version should it be merged to?

Copy link
Member
@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

OK for 3.4

@fabpot
Copy link
Member
fabpot commented Feb 19, 2018

Thank you @kbkk.

@fabpot fabpot closed this Feb 19, 2018
fabpot added a commit that referenced this pull request Feb 19, 2018
…t be created and type hint allows null (kbkk)

This PR was squashed before being merged into the 4.1-dev branch (closes #26140).

Discussion
----------

[Serializer] deserialize as a null when inner object cannot be created and type hint allows null

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT

```php
class ObjectConstructorDummy
{
    protected $foo;
    public $bar;
    private $baz;

    public function __construct($foo, $bar, $baz)
    {
        $this->foo = $foo;
        $this->bar = $bar;
        $this->baz = $baz;
    }
}

class DummyWithNullableConstructorObject
{
    private $id;
    private $inner;

    public function __construct($id, ?ObjectConstructorDummy $inner)
    {
        $this->id = $id;
        $this->inner = $inner;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getInner()
    {
        return $this->inner;
    }
}
```

Trying to deserialize to `DummyWithNullableConstructorObject` with the following data currently fails:
```php
[
    'id' => 10,
    'inner' => null
]
```

With this PR `DummyWithNullableConstructorObject ` would be constructed with `null` passed as `$inner` because of the type hint.

Commits
-------

2fe9eb1 [Serializer] deserialize as a null when inner object cannot be created and type hint allows null
@kbkk kbkk deleted the serializer-deserialize-nullable-objects-in-constructor branch February 20, 2018 15:04
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.

5 participants
0