diff --git a/src/Serializer/AbstractItemNormalizer.php b/src/Serializer/AbstractItemNormalizer.php index c08a967ad4..7048a67092 100644 --- a/src/Serializer/AbstractItemNormalizer.php +++ b/src/Serializer/AbstractItemNormalizer.php @@ -503,6 +503,10 @@ protected function getClassDiscriminatorResolvedClass(array $data, string $class } $type = $data[$mapping->getTypeProperty()] ?? $defaultType; + if (!\is_string($type)) { + throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type property "%s" for the abstract object "%s" must be a string.', $mapping->getTypeProperty(), $class), $type, ['string'], isset($context['deserialization_path']) ? $context['deserialization_path'].'.'.$mapping->getTypeProperty() : $mapping->getTypeProperty()); + } + if (null === ($mappedClass = $mapping->getClassForType($type))) { 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); } diff --git a/tests/Functional/DiscriminatedInputDtoTest.php b/tests/Functional/DiscriminatedInputDtoTest.php index ce4f3449a4..b597aa119d 100644 --- a/tests/Functional/DiscriminatedInputDtoTest.php +++ b/tests/Functional/DiscriminatedInputDtoTest.php @@ -49,6 +49,19 @@ public function testObjectTypedSubclassOnlyConstructorArgumentIsDenormalized(): ]); } + public function testNonStringTypePropertyIsRejected(): void + { + self::createClient()->request('POST', '/discriminated_notification_channels', [ + 'headers' => ['Content-Type' => 'application/ld+json'], + 'json' => [ + 'type' => ['webhook'], + 'config' => ['url' => 'https://example.com/hook'], + ], + ]); + + $this->assertResponseStatusCodeSame(400); + } + public function testObjectTypedSubclassOnlyPropertyIsDenormalizedOnPatch(): void { self::createClient()->request('PATCH', '/discriminated_notification_channels/1', [