From c3ddf55ddeb85f58543821833fa236230d73f03b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 28 Sep 2026 13:15:18 +0200 Subject: [PATCH] fix(serializer): 400 on non-string discriminator A non-string type property value reached ClassDiscriminatorMapping::getClassForType() and raised a TypeError, turning a malformed payload into a 500. --- src/Serializer/AbstractItemNormalizer.php | 4 ++++ tests/Functional/DiscriminatedInputDtoTest.php | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Serializer/AbstractItemNormalizer.php b/src/Serializer/AbstractItemNormalizer.php index c08a967ad45..7048a670927 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 ce4f3449a48..b597aa119de 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', [