Skip to content

fix(datastore): infer TYPE_ID only for valid signed 64-bit integers - #9873

Open
pentium10 wants to merge 1 commit into
googleapis:mainfrom
pentium10:fix/datastore-key-identifier-type
Open

pentium10 wants to merge 1 commit into
googleapis:mainfrom
pentium10:fix/datastore-key-identifier-type

Conversation

@pentium10

@pentium10 pentium10 commented Nov 29, 2026 •

Copy link
Copy Markdown

Fixes #9872

For humans

Problem

Google\Cloud\Datastore\Key::determineIdentifierType() previously relied on is_numeric() to decide whether an identifier should be inferred as Key::TYPE_ID (int64) or Key::TYPE_NAME (string).

is_numeric() returns true for values that cannot be represented as a signed 64-bit integer in Protobuf (PathElement.id), including:

  • All-digit strings exceeding signed 64-bit bounds (e.g. 32-digit MD5 hashes like '13975337727204308302793171870910')
  • Scientific-notation strings (e.g. md5('240610708') === '0e462097431906509019562988736854')
  • Float strings (e.g. '12.34')

Passing such values without an explicit identifierType caused a runtime crash during gRPC serialization (Google\Protobuf\Internal\Message::__construct(): Cannot convert '...' to integer).

Solution

  • Replaced the is_numeric() check in Key::determineIdentifierType() with a strict signed 64-bit integer check (Key::isInt64()), matching only int values or base-10 integer strings within [-9223372036854775808, 9223372036854775807].
  • Values outside that range (or non-integer numeric formats like floats / scientific notation) are now inferred as Key::TYPE_NAME.
  • Added unit tests in Datastore/tests/Unit/KeyTest.php covering int64 boundaries, overflowing numeric strings, scientific notation, float strings, and standard integer/string IDs.

Why not filter_var($identifier, FILTER_VALIDATE_INT)?

FILTER_VALIDATE_INT validates against PHP_INT_MAX/PHP_INT_MIN for the current platform. On 32-bit PHP (PHP_INT_SIZE === 4), standard 16-digit Datastore auto-allocated IDs (e.g. '5629499534213120') exceed 32-bit PHP_INT_MAX (2147483647) and would be misclassified as Key::TYPE_NAME. FILTER_VALIDATE_INT also trims whitespace and accepts bool/+ prefixes unless extra guards are added. A small string-based isInt64() check avoids all of these edge cases and works identically on both 32-bit and 64-bit PHP with no extra extensions.

Verification

  • Ran Datastore unit test suite (vendor/bin/phpunit tests/Unit): OK (362 tests, 918 assertions).

For agents

  • Files changed:
    • Datastore/src/Key.php: replaced is_numeric($identifier) in Key::determineIdentifierType() with Key::isInt64($identifier) (works identically on 32-bit and 64-bit PHP without extra extensions).
    • Datastore/tests/Unit/KeyTest.php: added testInferredIdentifierType() with inferredIdentifierTypeProvider() data provider.
  • Design note: filter_var(..., FILTER_VALIDATE_INT) was intentionally avoided because it bounds-checks against platform PHP_INT_MAX (breaking 16-digit Datastore int64 IDs on 32-bit PHP) and accepts whitespace / bool / + prefixes without additional guards.
  • Backward compatibility:
    • All valid signed 64-bit integers (int and base-10 string within [-9223372036854775808, 9223372036854775807]) continue to infer Key::TYPE_ID.
    • Explicit identifierType overrides (Key::TYPE_ID / Key::TYPE_NAME) are unaffected.
    • Only inputs that previously failed Protobuf int64 conversion now infer Key::TYPE_NAME.

@pentium10
pentium10 requested a review from a team as a code owner November 29, 2026 08:56
@product-auto-label product-auto-label Bot added the api: datastore Issues related to the Datastore API. label Nov 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: datastore Issues related to the Datastore API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Datastore] Key::determineIdentifierType() misclassifies overflowing numeric and scientific-notation strings as TYPE_ID

1 participant