Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[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 ourterms of service andprivacy 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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
4412ab4 to1dc1f0dCompare
sroze left a comment
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 moveunset($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.
komik966 commentedAug 26, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@sroze For me composition also would be better. I was thinking about introducing new |
366dd26 tobc24d62Comparedunglas commentedAug 26, 2018
👍 to move this in a dedicated class. It should be doable without BC break (initialize the new class in the constructor if not provided). |
komik966 commentedAug 26, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
When we introduce Second option is to create What are your thoughts? |
bc24d62 to6d471cfComparedunglas commentedAug 28, 2018
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 tod8905e2Comparekomik966 commentedAug 29, 2018
Ok, I'll prepare changes with this approach. |
d8905e2 to7b77f35Compare7b77f35 to2c37a8bComparefabpot commentedOct 10, 2018
@dunglas The code has changed since your last review. Can you check it again please? |
dunglas commentedOct 10, 2018
@fbourigault would you mind to take a look, as it is related with#28775 |
fbourigault commentedOct 10, 2018
What about introducing some |
komik966 commentedOct 10, 2018
I tried something similar (komik966@d734f32) but ended with lot of deprecations. |
komik966 commentedOct 10, 2018
I noticed that this code:
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.
| returnnull; | ||
| } | ||
| 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"?
fabpot commentedAug 6, 2019
What's the status here? |
dunglas commentedJan 15, 2020
@komik966 do you think it is worth it to finish this PR? If yes, could you rebase and fix the remaining comments please? |
dunglas commentedJan 15, 2020
Status: Needs Work |
komik966 commentedJan 15, 2020
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::instantiateObjectforces 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