Skip to content

fix(serializer): denormalize nullable collections of enums/objects#8381

Merged
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/8379-nullable-collection-denormalization
Jul 3, 2026
Merged

fix(serializer): denormalize nullable collections of enums/objects#8381
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/8379-nullable-collection-denormalization

Conversation

@soyuka

@soyuka soyuka commented Jul 3, 2026

Copy link
Copy Markdown
Member
Q A
Branch? 4.3
Bug fix? yes
New feature? no
Deprecations? no
Issues Fixes #8379
License MIT

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:

Doctrine\ORM\Mapping\ReflectionEnumProperty::getValue(): Argument #1 ($item) must be of type BackedEnum, string given

Root cause

Symfony's TypeInfo represents a nullable collection value type as a NullableType wrapping the real ObjectType/BackedEnumType. For a nullable Doctrine SIMPLE_ARRAY column mapped with enumType, DoctrineExtractor produces list<Enum|null>|null, i.e. the collection value type is NullableType(ObjectType).

AbstractItemNormalizer::createAndValidateAttributeValue() checked $collectionValueType instanceof ObjectType directly. Since NullableType does not extend ObjectType, that check evaluated to false, 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 WrappingTypeInterface before the instanceof ObjectType check, mirroring the existing unwrap loop already used later in the same method for the item type. Nested CollectionType wrapping is deliberately left untouched so a collection-of-collections is not misdetected as a collection-of-objects.

Tests

Added AbstractItemNormalizerTest::testDenormalizeNullableCollectionOfBackedEnums, which reproduces the NullableType(ObjectType) collection value type (as DoctrineExtractor emits it) and asserts the items are denormalized into enum instances. The test fails on 4.3 before the fix (items stay raw strings) and passes after.

Gates: php-cs-fixer clean, PHPStan clean on changed files, full src/Serializer/Tests suite green.

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
@soyuka soyuka merged commit 798b4cc into api-platform:4.3 Jul 3, 2026
109 of 112 checks passed
@soyuka soyuka deleted the fix/8379-nullable-collection-denormalization branch July 3, 2026 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant