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

Skip to content

[Serializer][Bug] deserialize into pre-existing objects in properties #31208

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
dkarlovi opened this issue Apr 23, 2019 · 13 comments
Closed

[Serializer][Bug] deserialize into pre-existing objects in properties #31208

dkarlovi opened this issue Apr 23, 2019 · 13 comments

Comments

@dkarlovi
Copy link
Contributor

dkarlovi commented Apr 23, 2019

Symfony version(s) affected: 4.2.3

Description

Assuming I have classes like this:

class Inner {
    public $freshInstance = true;
    public $name;
}

class Outer {
    /** @var Inner */
    public $inner;
}

and instance like

$inner = new Inner;
$inner->freshInstance = false;

$outer = new Outer;
$outer->inner = $inner;

When deserializing:

{
    "inner": {"name": "Inner Name"}
}

I'd expect the pre-existing Inner instance to be used, but a new one is created.

How to reproduce

https://github.com/dkarlovi/reproducer-symfony-31208

Possible Solution

Pre-populate OBJECT_TO_POPULATE with the pre-existing object if not-null?

@dkarlovi dkarlovi changed the title [Serializer] deserialize into pre-existing objects in properties [Serializer][Bug] deserialize into pre-existing objects in properties Apr 23, 2019
@dkarlovi
Copy link
Contributor Author

BTW I've hot-patched this just to test if it will work like this and it does:

class AbstractObjectNormalizer
{
    // ...

    protected function createChildContext(array $parentContext, $attribute): array
    {
        $context = parent::createChildContext($parentContext, $attribute);

        if (isset($parentContext[self::OBJECT_TO_POPULATE])) {
            $attribute = $this->getAttributeValue($parentContext[self::OBJECT_TO_POPULATE], $attribute);
            if (is_object($attribute)) {
                $context[self::OBJECT_TO_POPULATE] = $attribute;
            }
        }

        return $context;
    }
}

I'm sure @dunglas will have a better idea how to fix this.

@dunglas
Copy link
Member

dunglas commented Jul 9, 2019

Maybe could we reuse some logic of AbstractNormalizer#instantiateObject? Also probably related to #30956.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@dkarlovi
Copy link
Contributor Author

The issue was not tagged from a PR fixing it so I guess it's still relevant.

@carsonbot carsonbot removed the Stalled label Dec 21, 2020
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@dkarlovi
Copy link
Contributor Author

/carsonbot freeze ?

@carsonbot carsonbot removed the Stalled label Jun 22, 2021
@xabbuh
Copy link
Member

xabbuh commented Jun 26, 2021

@dkarlovi PR welcome if you would like to give it a try

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Just a quick reminder to make a comment on this. If I don't hear anything I'll close this.

@dkarlovi
Copy link
Contributor Author

I'm doing science and I'm still alive.

@carsonbot carsonbot removed the Stalled label Jan 10, 2022
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Friendly ping? Should this still be open? I will close if I don't hear anything.

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

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