Repository navigation
Description
For humans
Summary
Google\Cloud\Datastore\Key::determineIdentifierType() uses PHP's is_numeric() to infer whether a key identifier should be treated as Key::TYPE_ID (int64) or Key::TYPE_NAME (string).
Because is_numeric() returns true for values that cannot be represented as a Datastore 64-bit signed integer—such as 32-digit numeric MD5 hashes (e.g. '13975337727204308302793171870910'), scientific notation hashes (e.g. md5('240610708') === '0e462097431906509019562988736854'), or floats—the SDK assigns id instead of name, and later crashes inside Protobuf when constructing PathElement:
Exception: Cannot convert '13975337727204308302793171870910' to integer
at Google\Protobuf\Internal\Message::__construct(PathElement.php:64)
at Google\Cloud\Datastore\V1\Key\PathElement::__construct(PathElement.php:64)
at Google\Cloud\Datastore\Connection\Grpc::keysList(Grpc.php:387)
at Google\Cloud\Datastore\Connection\Grpc::lookup(Grpc.php:203)
Environment
google/cloud-datastore:v1.34.2google/protobuf:v5.36.2- PHP:
8.4-8.6
Steps to reproduce
<?php
use Google\Cloud\Datastore\Key;
use Google\Cloud\Datastore\V1\Key\PathElement;
$key = new Key('example-project');
// 32-digit numeric string (e.g. an all-digit MD5 hash) or scientific-notation MD5 like md5('240610708')
$key->pathElement('CacheEntry', '13975337727204308302793171870910');
// PathElement receives ['kind' => 'CacheEntry', 'id' => '13975337727204308302793171870910']
new PathElement($key->pathEnd()); // Throws: Exception: Cannot convert '13975337727204308302793171870910' to integerExpected behavior
When identifierType is omitted, Key::determineIdentifierType() should only infer Key::TYPE_ID if the value is a valid signed 64-bit integer (not a float, scientific notation string, or integer overflowing int64). Values that cannot fit in int64 should either fall back to Key::TYPE_NAME or fail fast with a clear InvalidArgumentException when constructing the Key rather than crashing later during gRPC serialization.
Workaround
Explicitly pass ['identifierType' => Key::TYPE_NAME] when calling DatastoreClient::key() or Key::pathElement().
For agents
- Repository / Package:
googleapis/google-cloud-php(Datastorecomponent,google/cloud-datastore) - Affected file & method:
Datastore/src/Key.php->Key::determineIdentifierType($identifier, $identifierType) - Current logic:
if (is_numeric($identifier)) { return self::TYPE_ID; } return self::TYPE_NAME;
- Failure mode:
is_numeric()accepts floats ('12.34'), scientific notation ('0e462097431906509019562988736854'), and out-of-range integer strings ('13975337727204308302793171870910').Google\Cloud\Datastore\V1\Key\PathElementdefinesidas protobufint64, soGoogle\Protobuf\Internal\Message::__construct()throwsException: Cannot convert '...' to integerat RPC time (Grpc::keysList()/Grpc::lookup()). - Suggested fix direction:
- Replace
is_numeric($identifier)inKey::determineIdentifierType()with a strict signed 64-bit integer check (e.g.,is_int($identifier)or a regex/range check matching a canonical base-10 integer within[-9223372036854775808, 9223372036854775807], excluding floats, scientific notation, and overflowing digit strings), or validateTYPE_IDbounds inKey::normalizeElement(). - Add unit tests in
Datastore/tests/Unit/KeyTest.phpcovering:- 32-digit numeric strings exceeding
PHP_INT_MAX('13975337727204308302793171870910') - Scientific-notation strings (
'0e462097431906509019562988736854') - Float strings (
'12.34') - Valid 64-bit integer IDs as both
intandstring(preserving existing behavior).
- 32-digit numeric strings exceeding
- Replace
Activity