Skip to content

Commit 74f667d

Browse files
authored
Merge pull request #5644 from TeBoring/fix-4765
Convert integer to string if field is string field in json
2 parents 32339be + 8400e29 commit 74f667d

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

conformance/failure_list_php.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Required.Proto3.JsonInput.FloatFieldTooSmall
1313
Required.Proto3.JsonInput.DoubleFieldTooSmall
1414
Required.Proto3.JsonInput.Int32FieldNotInteger
1515
Required.Proto3.JsonInput.Int64FieldNotInteger
16+
Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotInt
17+
Required.Proto3.JsonInput.StringFieldNotAString
1618
Required.Proto3.JsonInput.Uint32FieldNotInteger
1719
Required.Proto3.JsonInput.Uint64FieldNotInteger
1820
Required.Proto3.JsonInput.Int32FieldLeadingSpace

php/src/Google/Protobuf/Internal/Message.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,9 @@ private function convertJsonValueToProtoValue(
832832
if (is_null($value)) {
833833
return $this->defaultValue($field);
834834
}
835+
if (is_numeric($value)) {
836+
return strval($value);
837+
}
835838
if (!is_string($value)) {
836839
throw new GPBDecodeException(
837840
"String field only accepts string value");

php/tests/encode_decode_test.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,4 +1148,14 @@ public function testJsonDecodeMapWithDefaultValueKey()
11481148
$m->serializeToJsonString());
11491149
}
11501150

1151+
public function testJsonDecodeNumericStringMapKey()
1152+
{
1153+
$m = new TestMessage();
1154+
$m->getMapStringString()["1"] = "1";
1155+
$data = $m->serializeToJsonString();
1156+
$this->assertSame("{\"mapStringString\":{\"1\":\"1\"}}", $data);
1157+
$n = new TestMessage();
1158+
$n->mergeFromJsonString($data);
1159+
}
1160+
11511161
}

tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ build_php5.6_mac() {
482482
export PATH="$PHP_FOLDER/bin:$PATH"
483483

484484
# Install phpunit
485-
curl https://phar.phpunit.de/phpunit-5.6.10.phar -L -o phpunit.phar
485+
curl https://phar.phpunit.de/phpunit-5.6.8.phar -L -o phpunit.phar
486486
chmod +x phpunit.phar
487487
sudo mv phpunit.phar /usr/local/bin/phpunit
488488

0 commit comments

Comments
 (0)