From 96029f3c4aef2fe6ce3ab7c40f3bbbe985356708 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Mon, 28 Jan 2019 08:51:45 +0000 Subject: [PATCH 1/3] Convert integer to string if field is string field in json json_decode automatically convert numeric string to integer, so that we need to convert it back. However, this will suceed to parse invalid json data with string field set as integer even though it should have failed. Because, the failure case is less often than the succeeding case, we decided to make this change. Users should make sure their data don't use integer for string fields by themselves. --- conformance/failure_list_php.txt | 2 ++ php/src/Google/Protobuf/Internal/Message.php | 3 +++ php/tests/encode_decode_test.php | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/conformance/failure_list_php.txt b/conformance/failure_list_php.txt index 0d2341127e..8bd8a99ea4 100644 --- a/conformance/failure_list_php.txt +++ b/conformance/failure_list_php.txt @@ -13,6 +13,8 @@ Required.Proto3.JsonInput.FloatFieldTooSmall Required.Proto3.JsonInput.DoubleFieldTooSmall Required.Proto3.JsonInput.Int32FieldNotInteger Required.Proto3.JsonInput.Int64FieldNotInteger +Required.Proto3.JsonInput.RepeatedFieldWrongElementTypeExpectingStringsGotInt +Required.Proto3.JsonInput.StringFieldNotAString Required.Proto3.JsonInput.Uint32FieldNotInteger Required.Proto3.JsonInput.Uint64FieldNotInteger Required.Proto3.JsonInput.Int32FieldLeadingSpace diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index d304a12b47..2c41c7aa75 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.php @@ -832,6 +832,9 @@ class Message if (is_null($value)) { return $this->defaultValue($field); } + if (is_numeric($value)) { + return strval($value); + } if (!is_string($value)) { throw new GPBDecodeException( "String field only accepts string value"); diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php index 57b1bf1f5f..1325db2cf8 100644 --- a/php/tests/encode_decode_test.php +++ b/php/tests/encode_decode_test.php @@ -1148,4 +1148,14 @@ class EncodeDecodeTest extends TestBase $m->serializeToJsonString()); } + public function testJsonDecodeNumericStringMapKey() + { + $m = new TestMessage(); + $m->getMapStringString()["1"] = "1"; + $data = $m->serializeToJsonString(); + $this->assertSame("{\"mapStringString\":{\"1\":\"1\"}}", $data); + $n = new TestMessage(); + $n->mergeFromJsonString($data); + } + } From 71e5de01d3fe49bfc29a55b190f6d066cd5e8222 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Mon, 28 Jan 2019 19:24:04 +0000 Subject: [PATCH 2/3] phpunit-5.6.10 link is broken Use 5.6.8 instead --- tests.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests.sh b/tests.sh index 2f453bdc80..c5e5188ece 100755 --- a/tests.sh +++ b/tests.sh @@ -482,8 +482,7 @@ build_php5.6_mac() { export PATH="$PHP_FOLDER/bin:$PATH" # Install phpunit - curl https://phar.phpunit.de/phpunit-5.6.10.phar -L -o phpunit.phar - chmod +x phpunit.phar + curl https://phar.phpunit.de/phpunit-5.6.8.phar -L -o phpunit.phar chmod +x phpunit.phar sudo mv phpunit.phar /usr/local/bin/phpunit # Install valgrind From 8400e29044cdc4ab45e514650cb6b65000588401 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Mon, 28 Jan 2019 20:58:58 +0000 Subject: [PATCH 3/3] Divide commands into separate lines --- tests.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests.sh b/tests.sh index c5e5188ece..618b6864fd 100755 --- a/tests.sh +++ b/tests.sh @@ -482,7 +482,8 @@ build_php5.6_mac() { export PATH="$PHP_FOLDER/bin:$PATH" # Install phpunit - curl https://phar.phpunit.de/phpunit-5.6.8.phar -L -o phpunit.phar chmod +x phpunit.phar + curl https://phar.phpunit.de/phpunit-5.6.8.phar -L -o phpunit.phar + chmod +x phpunit.phar sudo mv phpunit.phar /usr/local/bin/phpunit # Install valgrind