Skip to content

fix(serializer): don't leak relation_native_type into embedded documents - #8508

Merged
soyuka merged 1 commit into
api-platform:4.3from
Kamhal24:fix/serializer-relation-native-type-embedded-documents
Sep 9, 2026
Merged

fix(serializer): don't leak relation_native_type into embedded documents#8508
soyuka merged 1 commit into
api-platform:4.3from
Kamhal24:fix/serializer-relation-native-type-embedded-documents

Conversation

@Kamhal24

@Kamhal24 Kamhal24 commented Sep 8, 2026

Copy link
Copy Markdown
Q A
Branch? 4.3
Tickets Fixes #8507
License MIT
Doc PR n/a

Since 4.3.15 the relation_native_type context key (set for a relation in createAndValidateAttributeValue(), read by the IRI type-confusion guard in getResourceFromIri()) is never cleared when an embedded document is denormalized. It leaks through the embedded resource and any plain DTOs below it, so a nested IRI is validated against the outer relation's declared type and rejected with 400 Invalid IRI, even though it resolves to exactly the declared class.

Shape: resource A → embedded resource collection B (writableLink: true) → plain DTO collection C → IRI to resource D. Details and root-cause walkthrough in #8507.

Fix: in AbstractItemNormalizer::denormalize(), once the data is an embedded document (after the \is_string($data) IRI branch), drop the key so children start clean. Relations inside the embedded document still set their own relation_native_type before resolving their IRIs, so the union/type-confusion guards keep working (UnionIriCollectionTest, TypeConfusionRelationIriTest unchanged and green).

Tests: tests/Functional/Serializer/EmbeddedRelationNativeTypeTest.php with Doctrine-free fixtures under tests/Fixtures/TestBundle/ApiResource/EmbeddedRelationNativeType/ (resource → embedded resource collection → plain DTO collection → IRI):

  • valid nested IRI: 400 Invalid IRI "/embedded_relation_targets/1". before the fix, 201 after;
  • an IRI of the wrong resource class in the same nested position is still rejected with 400, so the type-confusion guard keeps working below embedded documents.

Also verified against a 2955-test downstream functional suite on 4.3.18.

Since 4.3.15 the relation_native_type context key set for a relation in
createAndValidateAttributeValue() was never cleared when an embedded document
was denormalized. It leaked through the embedded resource and any plain DTOs
below it, so a nested IRI was validated by getResourceFromIri() against the
outer relation's declared type and rejected with 400 "Invalid IRI" even though
it resolved to exactly the declared class.

Clear the key in denormalize() once the data is an embedded document: relations
inside it set their own relation_native_type before resolving their IRIs, so
the union and type-confusion guards keep working.

Fixes api-platform#8507
@soyuka
soyuka merged commit 40165b6 into api-platform:4.3 Sep 9, 2026
112 checks passed
@soyuka

soyuka commented Sep 9, 2026

Copy link
Copy Markdown
Member

thanks for this! does this also fixes #8503 ?

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.

2 participants