-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Argument objects #19277
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
Changes from 1 commit
7bd4ac5
e99a90b
e64e999
4884a2e
f361e52
f46a176
93608dc
d4cdb00
5556fa5
3fe9802
e437e04
7b5d55d
98bcb91
988eba1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -281,12 +281,13 @@ protected function getConstructor(array &$data, $class, array &$context, \Reflec | |
* @param array $context | ||
* @param \ReflectionClass $reflectionClass | ||
* @param array|bool $allowedAttributes | ||
* @param string|null $format | ||
* | ||
* @return object | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes) | ||
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, $format = null) | ||
{ | ||
if ( | ||
isset($context[static::OBJECT_TO_POPULATE]) && | ||
|
@@ -320,12 +321,13 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref | |
} | ||
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) { | ||
$parameterData = $data[$key]; | ||
if (null !== $constructogrParameter->getClass()) { | ||
if (null !== $constructorParameter->getClass()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally find it easier to check check if the class is null or not in this case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree but the type hint may be on a class not loadable. In this case, the exception thrown should be catched and the default value applied. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you elaborate? On which scenario may it happen? Not that I'm not trusting you, but if that's the case I would like to add a test for it :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case you use a type hint with a class from a third party library which is not loaded because you don't need it for example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see, fair enough will change that then |
||
$parameterData = $this->serializer->denormalize($parameterData, $constructorParameter->getClass()->getName(), null, $context); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgot this comment, it would be a BC break: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dunglas I'm doing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe can you add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cant' we make the normalizer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better. But it should be done in another PR. |
||
} | ||
|
||
// Don't run set for a parameter passed to the constructor | ||
$params[] = $parameterData; | ||
unset($data[$key]); | ||
} elseif ($constructorParameter->isDefaultValueAvailable()) { | ||
$params[] = $constructorParameter->getDefaultValue(); | ||
} else { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry I realized that this is a BC break... It should be removed. But it's annoying because some normalizers may depend of the format (normalizers of API Platform for instance).
An alternative approach:
instantiateComplexObject
(with the new parameter, it can be movec before$context
for consistency)instantiateObject
with the current signature and callinstantiateComplexObject
inside it with format asnull
instantiateObject
WDYT?
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.
will do