Skip to content

fix(elasticsearch): coerce document _id to declared int identifier type#8296

Open
soyuka wants to merge 1 commit into
api-platform:4.3from
soyuka:fix/elasticsearch-id-denorm-int-8010
Open

fix(elasticsearch): coerce document _id to declared int identifier type#8296
soyuka wants to merge 1 commit into
api-platform:4.3from
soyuka:fix/elasticsearch-id-denorm-int-8010

Conversation

@soyuka

@soyuka soyuka commented Jun 12, 2026

Copy link
Copy Markdown
Member

Summary

Elasticsearch always exposes the document identifier (_id) as a string. DocumentNormalizer::populateIdentifier() wrote that raw string into _source under the resource's identifier key, so a resource whose identifier is declared int failed denormalization in the inner ObjectNormalizer with NotNormalizableValueException (The type of the "id" attribute ... must be one of "int" ("string" given)).

The normalizer now coerces _id to int only when the identifier's declared native type is int (and not string). String and UUID identifiers are left untouched. The coercion is type-driven via PropertyMetadataFactoryInterface::create(...)->getNativeType(), not a blind cast; when the property metadata factory or type info is unavailable the value is returned unchanged (no behavior change).

Reproduction

Denormalizing an ES document with a string _id into a resource whose identifier is ?int threw NotNormalizableValueException instead of building the item.

Test plan

  • Added failing test testDenormalizeCoercesIdentifierToDeclaredType (red before fix, green after).
  • Full Elasticsearch test suite passes locally.
  • php-cs-fixer + PHPStan clean; the container compiles with the new (nullable, last-position, BC-safe) constructor argument.

Fixes #8010

Elasticsearch always exposes _id as a string; DocumentNormalizer wrote it verbatim into _source, so an int-typed resource identifier failed denormalization with NotNormalizableValueException. Coerce _id to int only when the declared native identifier type is int; string and UUID identifiers are left untouched.

Fixes api-platform#8010
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