Skip to content

fix(serializer): allow nullable to-many relations to normalize as null#8254

Merged
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/nullable-collection-normalize-7050
Jun 5, 2026
Merged

fix(serializer): allow nullable to-many relations to normalize as null#8254
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/nullable-collection-normalize-7050

Conversation

@soyuka
Copy link
Copy Markdown
Member

@soyuka soyuka commented Jun 5, 2026

Summary

AbstractItemNormalizer::getAttributeValue() rejected any non-iterable value on a to-many branch with UnexpectedValueException('Unexpected non-iterable value for to-many relation.') — including null. A property typed as a nullable collection of resources crashed during normalization. The to-one branch already accepted null; mirror that behavior on to-many and return null when the property is nullable.

Reproduction

A resource exposing ?Collection<Item> $items = null crashed when serialized with items set to null.

Test plan

  • Added test asserting a nullable to-many relation normalizes to null.
  • Existing AbstractItemNormalizerTest cases still pass.
  • No regression in the surrounding suite.

Fixes #7050

A property typed as a nullable collection of resources (e.g. ?array<Foo>
or Collection|null) raised UnexpectedValueException during normalization
because getAttributeValue() rejected any non-iterable value, including
null. The to-one branch already accepted null; mirror that behavior on
the to-many branch and return null when the property is nullable and
the value is null.

Fixes api-platform#7050
@soyuka soyuka force-pushed the fix/nullable-collection-normalize-7050 branch from 763c386 to c101ff1 Compare June 5, 2026 11:48
@soyuka soyuka merged commit d5d8176 into api-platform:4.3 Jun 5, 2026
109 of 112 checks passed
@soyuka soyuka deleted the fix/nullable-collection-normalize-7050 branch June 5, 2026 12:08
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