diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index 64aadf9d26..e74943c1ab 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.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()) { diff --git a/php/tests/EncodeDecodeTest.php b/php/tests/EncodeDecodeTest.php index d471a5a17a..b7c1ab24ca 100644 --- a/php/tests/EncodeDecodeTest.php +++ b/php/tests/EncodeDecodeTest.php @@ -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();