Skip to content

fix(serializer): backport handling of union/intersection type in item normalizer #7106

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
$propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $this->getFactoryOptions($context));
$types = $propertyMetadata->getBuiltinTypes() ?? [];
$isMultipleTypes = \count($types) > 1;
$denormalizationException = null;

foreach ($types as $type) {
if (null === $value && ($type->isNullable() || ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false))) {
Expand All @@ -908,7 +909,18 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
$context['resource_class'] = $resourceClass;
unset($context['uri_variables']);

return $this->denormalizeCollection($attribute, $propertyMetadata, $type, $resourceClass, $value, $format, $context);
try {
return $this->denormalizeCollection($attribute, $propertyMetadata, $type, $resourceClass, $value, $format, $context);
} catch (NotNormalizableValueException $e) {
// union/intersect types: try the next type, if not valid, an exception will be thrown at the end
if ($isMultipleTypes) {
$denormalizationException ??= $e;

continue;
}

throw $e;
}
}

if (
Expand All @@ -918,7 +930,18 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
$resourceClass = $this->resourceClassResolver->getResourceClass(null, $className);
$childContext = $this->createChildContext($this->createOperationContext($context, $resourceClass), $attribute, $format);

return $this->denormalizeRelation($attribute, $propertyMetadata, $resourceClass, $value, $format, $childContext);
try {
return $this->denormalizeRelation($attribute, $propertyMetadata, $resourceClass, $value, $format, $childContext);
} catch (NotNormalizableValueException $e) {
// union/intersect types: try the next type, if not valid, an exception will be thrown at the end
if ($isMultipleTypes) {
$denormalizationException ??= $e;

continue;
}

throw $e;
}
}

if (
Expand All @@ -933,7 +956,18 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value

unset($context['resource_class'], $context['uri_variables']);

return $this->serializer->denormalize($value, $className.'[]', $format, $context);
try {
return $this->serializer->denormalize($value, $className.'[]', $format, $context);
} catch (NotNormalizableValueException $e) {
// union/intersect types: try the next type, if not valid, an exception will be thrown at the end
if ($isMultipleTypes) {
$denormalizationException ??= $e;

continue;
}

throw $e;
}
}

if (null !== $className = $type->getClassName()) {
Expand All @@ -943,7 +977,18 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value

unset($context['resource_class'], $context['uri_variables']);

return $this->serializer->denormalize($value, $className, $format, $context);
try {
return $this->serializer->denormalize($value, $className, $format, $context);
} catch (NotNormalizableValueException $e) {
// union/intersect types: try the next type, if not valid, an exception will be thrown at the end
if ($isMultipleTypes) {
$denormalizationException ??= $e;

continue;
}

throw $e;
}
}

/* From @see AbstractObjectNormalizer::validateAndDenormalize() */
Expand Down Expand Up @@ -1019,6 +1064,10 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
}
}

if ($denormalizationException) {
throw $denormalizationException;
}

return $value;
}

Expand Down
Loading