-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add support to only check first element of $data in ArrayDenormalizer #45337
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,10 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Denormaliz | |
{ | ||
use DenormalizerAwareTrait; | ||
|
||
public function __construct(private bool $checkFirstElement = false) | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
|
@@ -69,8 +73,16 @@ public function supportsDenormalization(mixed $data, string $type, string $forma | |
throw new BadMethodCallException(sprintf('The nested denormalizer needs to be set to allow "%s()" to be used.', __METHOD__)); | ||
} | ||
|
||
return str_ends_with($type, '[]') | ||
&& $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context); | ||
if (!str_ends_with($type, '[]')) { | ||
return false; | ||
} | ||
|
||
if ($this->checkFirstElement) { | ||
$data = \is_array($data) ? $data : []; | ||
$data = 0 === \count($data) ? null : reset($data); | ||
CipherdevNL marked this conversation as resolved.
Show resolved
Hide resolved
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. This basically means that we would call the nested denormalizers with
Since we're attempting to remove odd behevior, I'd like to get it right this time. 😉 Suggestion: If the new mode is active and 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. Totally agree of getting it right directly :). You're right, passing null might indeed be odd for denomalizers and may introduce other unwanted issues. And incase The only side effect would be, when an unsupported type is given and Think this side effect is only preventable by deprecating/ removing And incase we might want to introduce BC for 7.0 we can consider this option like already discussed in the issue itself. 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 totally agree with @derrabus there. |
||
} | ||
|
||
return $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context); | ||
} | ||
|
||
/** | ||
|
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.
Here, if
checkFirstElement
is true and we are giving anint
for example, we will convert the data to an empty array, which is quite odd to me.What about returning false in that case?