fix(serializer): denormalize nullable collections of enums/objects#8381
Merged
soyuka merged 1 commit intoJul 3, 2026
Merged
Conversation
Symfony's TypeInfo represents a nullable collection value type (e.g. an array of a nullable BackedEnum, as produced by Doctrine's DoctrineExtractor for a nullable SIMPLE_ARRAY column with "enumType") as a NullableType wrapping the real ObjectType/BackedEnumType. AbstractItemNormalizer::createAndValidateAttributeValue() checked `$collectionValueType instanceof ObjectType` directly, which is false for a NullableType, so the collection-of-objects branch was skipped and the raw scalar values (e.g. "email") were left untouched instead of being denormalized into enum/object instances. This later caused Doctrine errors such as: ReflectionEnumProperty::getValue(): Argument #1 ($item) must be of type BackedEnum, string given Unwrap the collection value type through WrappingTypeInterface before the instanceof check, mirroring the existing unwrap loop used later in the same method for the item type. Nested CollectionType wrapping is deliberately left untouched so a collection-of-collections isn't misdetected as a collection-of-objects. Fixes api-platform#8379
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Nullable arrays/collections of BackedEnums or objects fail to deserialize on POST/PATCH: the elements are left as raw strings instead of being denormalized into enum/object instances, which later triggers a Doctrine error such as:
Root cause
Symfony's TypeInfo represents a nullable collection value type as a
NullableTypewrapping the realObjectType/BackedEnumType. For a nullable DoctrineSIMPLE_ARRAYcolumn mapped withenumType,DoctrineExtractorproduceslist<Enum|null>|null, i.e. the collection value type isNullableType(ObjectType).AbstractItemNormalizer::createAndValidateAttributeValue()checked$collectionValueType instanceof ObjectTypedirectly. SinceNullableTypedoes not extendObjectType, that check evaluated tofalse, the collection-of-objects branch was skipped, and the items were never handed off to the serializer for enum/object denormalization.Fix
Unwrap the collection value type through
WrappingTypeInterfacebefore theinstanceof ObjectTypecheck, mirroring the existing unwrap loop already used later in the same method for the item type. NestedCollectionTypewrapping is deliberately left untouched so a collection-of-collections is not misdetected as a collection-of-objects.Tests
Added
AbstractItemNormalizerTest::testDenormalizeNullableCollectionOfBackedEnums, which reproduces theNullableType(ObjectType)collection value type (asDoctrineExtractoremits it) and asserts the items are denormalized into enum instances. The test fails on4.3before the fix (items stay raw strings) and passes after.Gates: php-cs-fixer clean, PHPStan clean on changed files, full
src/Serializer/Testssuite green.