-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] add method for preparing constructor argument #28263
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
[Serializer] add method for preparing constructor argument #28263
Conversation
4412ab4
to
1dc1f0d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not fond of inheritance-based extension points but that's mostly how it is done in the Serializer component so far so 👍 for me.
$parameterData = $data[$key]; | ||
if (null === $parameterData && $constructorParameter->allowsNull()) { | ||
// Don't run set for a parameter passed to the constructor | ||
unset($data[$key]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The &$data
argument makes little sense to me (especially given the name of the method). Can you move unset($data[$key])
in the caller instead and directly pass $parameterData
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Fixed. Maybe w can do similar with $key
parameter? It's only used in exception message. Unless it's important to show normalized constructor parameter name, we can use $constructorParameter->name
.
@sroze For me composition also would be better. I was thinking about introducing new |
366dd26
to
bc24d62
Compare
👍 to move this in a dedicated class. It should be doable without BC break (initialize the new class in the constructor if not provided). |
When we introduce Second option is to create What are your thoughts? |
bc24d62
to
6d471cf
Compare
I'm for option 1, it will be more flexible, and I want to break the serializer in smaller classes since a long time. |
6d471cf
to
d8905e2
Compare
Ok, I'll prepare changes with this approach. |
d8905e2
to
7b77f35
Compare
7b77f35
to
2c37a8b
Compare
@dunglas The code has changed since your last review. Can you check it again please? |
@fbourigault would you mind to take a look, as it is related with #28775 |
What about introducing some |
I tried something similar (komik966@d734f32) but ended with lot of deprecations. |
I noticed that this code:
symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php Line 299 in 0ea1adf
Should we treat creating arguments for constructor differently than for other methods (setters)? |
* @param array $context | ||
* @param string|null $format | ||
* | ||
* @return mixed value of constructor parameter - an argument |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the docblock lines above provide enough value to be kept, I'd personally suggest to remove them.
return null; | ||
} | ||
try { | ||
if (null !== $constructorParameter->getClass()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like this could be turned to
if (null === $constructorParameter->getClass()) {
return $parameterData;
}
and allow unindenting the block currently inside the "if"?
What's the status here? |
@komik966 do you think it is worth it to finish this PR? If yes, could you rebase and fix the remaining comments please? |
Status: Needs Work |
I hasn't followed what's happening in serializer and api-platform for one year. So it's safer to abandon this PR. |
When we denormalize objects which classes have constructor, it becomes useful to add custom logic for creating constructor arguments from decoded data. E. g. constructor parameter is type hinted as class, but decoded data (coming from rest api) is IRI string.
Current implemetation of
Symfony\Component\Serializer\Normalizer\AbstractNormalizer::instantiateObject
forces us to think about other aspects of instantiating object (e.g. discriminators, context) when overriding it.Related discussions: api-platform/core#1749, api-platform/core#2178