Skip to content

Commit f4e3c4a

Browse files
committed
feat(serializer): handle defaultType for DiscriminatorMap
1 parent d3b98b5 commit f4e3c4a

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/Serializer/AbstractItemNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,12 @@ protected function getClassDiscriminatorResolvedClass(array $data, string $class
399399
return $class;
400400
}
401401

402-
if (!isset($data[$mapping->getTypeProperty()])) {
402+
$defaultType = method_exists($mapping, 'getDefaultType') ? $mapping->getDefaultType() : null;
403+
if (!isset($data[$mapping->getTypeProperty()]) && null === $defaultType) {
403404
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Type property "%s" not found for the abstract object "%s".', $mapping->getTypeProperty(), $class), null, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty());
404405
}
405406

406-
$type = $data[$mapping->getTypeProperty()];
407+
$type = $data[$mapping->getTypeProperty()] ?? $defaultType;
407408
if (null === ($mappedClass = $mapping->getClassForType($type))) {
408409
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type "%s" is not a valid value.', $type), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty(), true);
409410
}

0 commit comments

Comments
 (0)