Skip to content

Commit

Permalink
Merge pull request #5644 from TeBoring/fix-4765
Browse files Browse the repository at this point in the history
Convert integer to string if field is string field in json
  • Loading branch information
TeBoring authored Jan 28, 2019
2 parents 32339be + 8400e29 commit 74f667d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions conformance/failure_list_php.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions php/src/Google/Protobuf/Internal/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,9 @@ private function convertJsonValueToProtoValue(
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");
Expand Down
10 changes: 10 additions & 0 deletions php/tests/encode_decode_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1148,4 +1148,14 @@ public function testJsonDecodeMapWithDefaultValueKey()
$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);
}

}
2 changes: 1 addition & 1 deletion tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +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
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

Expand Down

0 comments on commit 74f667d

Please sign in to comment.