Skip to content

Commit

Permalink
Fix php json parsing not throwing error for unknown field. (#8528)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeBoring committed May 4, 2021
1 parent 28101c3 commit 114efc4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion php/src/Google/Protobuf/Internal/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,7 @@ protected function mergeFromJsonArray($array, $ignore_unknown)
$v->mergeFromJsonArray($value, $ignore_unknown);
$fields[$key] = $v;
}
return;
}
if (is_a($this, "Google\Protobuf\Value")) {
if (is_bool($array)) {
Expand Down Expand Up @@ -1241,7 +1242,13 @@ private function mergeFromArrayJsonImpl($array, $ignore_unknown)
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()) {
Expand Down
14 changes: 14 additions & 0 deletions php/tests/EncodeDecodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public function testDecodeJsonSimple()
$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();
Expand Down

0 comments on commit 114efc4

Please sign in to comment.