Skip to content

[Serializer] Add Support for object_to_populate in CustomNormalizer #21716

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
wants to merge 3 commits into from

Conversation

chrisguitarguy
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #21715
License MIT
Doc PR n/a

This pulls a trait out of AbstractNormalizer with a method to extract the object to populate and adds some tests for it. Then uses that trait in both AbstractNormalizer and CustomNormalizer so both can support the object_to_populate key.

@chrisguitarguy chrisguitarguy changed the title [Serializer] Add Support for object_to_populate to CustomNormalizer [Serializer] Add Support for object_to_populate in CustomNormalizer Feb 22, 2017
$context[static::OBJECT_TO_POPULATE] instanceof $class
) {
$object = $context[static::OBJECT_TO_POPULATE];
if (null !== $object = $this->extractObjectToPopulate($class, $context, static::OBJECT_TO_POPULATE)) {
unset($context[static::OBJECT_TO_POPULATE]);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this line be moved in in the trait too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would involve passing context by reference into the trait. That's the only reason I didn't do it.

return $context[$key];
}

return null;
Copy link
Member

Choose a reason for hiding this comment

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

Useless, can be removed.

* @param string $class The class the object should be
* @param $context The denormalization context
* @param string $key They in which to look for the object to populate.
* Keeps backwards compatibility with `AbstractNormalizer`.
Copy link
Member

Choose a reason for hiding this comment

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

There is no reason to mention this in the comment IMO.

Copy link

@felipsmartins felipsmartins Feb 24, 2017

Choose a reason for hiding this comment

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

Why not?

@dunglas
Copy link
Member

dunglas commented Feb 24, 2017

Just asking, what is the use case of this class? I think that it's always better to create custom normalizers (implementing the NormalizerInterface than adding the serialization logic in the class itself.

I would be in favor of deprecating this class now and remove it from Symfony 4.

@chrisguitarguy
Copy link
Contributor Author

chrisguitarguy commented Feb 24, 2017

Just asking, what is the use case of this class?

In my case, I was interested in using it with a "dumb" dto-like object.

class SomeDto implements DenormalizableInterface
{
  // ...
}

The idea being to keep everything close together: the normalization, denormalization, and the DTO itself. So basically what you are against :)

I was interested in doing this because I think it's a lot nicer developer experience to keep the normalization/denormalization closer to the object being serialized/deserialized. Easier to understand and catch at a glance. Doesn't require you to register services in the container with a special tag, etc.

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Feb 25, 2017
@fabpot
Copy link
Member

fabpot commented Sep 26, 2017

@chrisguitarguy Can you please rebase it so that we can merge it? Thanks. Base should also be changed to 3.4.

And use that trait in `AbstractNormalizer`.
And fallback to the default `new $class();` behavior.
@chrisguitarguy chrisguitarguy changed the base branch from master to 3.4 September 27, 2017 00:33
@chrisguitarguy
Copy link
Contributor Author

chrisguitarguy commented Sep 27, 2017

@fabpot rebased onto 3.4. Did you want me to make any of the changes @dunglas suggested?

@fabpot
Copy link
Member

fabpot commented Sep 27, 2017

Looks like he approved this PR without any further changes :)

@fabpot
Copy link
Member

fabpot commented Sep 27, 2017

Thank you @chrisguitarguy.

fabpot added a commit that referenced this pull request Sep 27, 2017
…ustomNormalizer (chrisguitarguy)

This PR was squashed before being merged into the 3.4 branch (closes #21716).

Discussion
----------

[Serializer] Add Support for `object_to_populate` in CustomNormalizer

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21715
| License       | MIT
| Doc PR        | n/a

This pulls a trait out of `AbstractNormalizer` with a method to extract the object to populate and adds some tests for it. Then uses that trait in both `AbstractNormalizer` and `CustomNormalizer` so both can support the `object_to_populate` key.

Commits
-------

ec9242d [Serializer] Add Support for  in CustomNormalizer
@fabpot fabpot closed this Sep 27, 2017
This was referenced Oct 18, 2017
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.

6 participants