Fix php json parsing not throwing error for unknown field. (#8528)

pull/8566/head
Paul Yang 4 years ago committed by GitHub
parent 28101c3e39
commit 114efc42d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      php/src/Google/Protobuf/Internal/Message.php
  2. 14
      php/tests/EncodeDecodeTest.php

@ -1188,6 +1188,7 @@ class Message
$v->mergeFromJsonArray($value, $ignore_unknown);
$fields[$key] = $v;
}
return;
}
if (is_a($this, "Google\Protobuf\Value")) {
if (is_bool($array)) {
@ -1241,7 +1242,13 @@ class Message
if (is_null($field)) {
$field = $this->desc->getFieldByName($key);
if (is_null($field)) {
continue;
if ($ignore_unknown) {
continue;
} else {
throw new GPBDecodeException(
$key . ' is unknown.'
);
}
}
}
if ($field->isMap()) {

@ -45,6 +45,20 @@ class EncodeDecodeTest extends TestBase
$this->assertEquals(1, $m->getOptionalInt32());
}
public function testDecodeJsonUnknown()
{
$this->expectException(Exception::class);
$m = new TestMessage();
$m->mergeFromJsonString("{\"unknown\":1}");
}
public function testDecodeJsonIgnoreUnknown()
{
$m = new TestMessage();
$m->mergeFromJsonString("{\"unknown\":1}", true);
}
public function testDecodeTopLevelBoolValue()
{
$m = new BoolValue();

Loading…
Cancel
Save