From 09295965758bd92764dbad18fa26d5458ece878f Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Thu, 30 Jul 2020 12:00:13 -0700 Subject: [PATCH] Check realoneof in json parsing (#7763) * Check realoneof in json parsing * Fix whichoneof to also work for synthetic oneof --- .gitignore | 3 +++ php/ext/google/protobuf/php-upb.c | 8 ++++++-- php/tests/encode_decode_test.php | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a7349c2aa8..7235f6b78b 100644 --- a/.gitignore +++ b/.gitignore @@ -144,12 +144,15 @@ php/tests/core php/tests/vgcore* php/tests/multirequest.result php/tests/nohup.out +php/tests/.phpunit.result.cache +php/tests/phpunit-* php/ext/google/protobuf/.libs/ php/ext/google/protobuf/Makefile.fragments php/ext/google/protobuf/Makefile.global php/ext/google/protobuf/Makefile.objects php/ext/google/protobuf/acinclude.m4 php/ext/google/protobuf/build/ +php/ext/google/protobuf/bundled_php.c php/ext/google/protobuf/config.h php/ext/google/protobuf/config.h.in~ php/ext/google/protobuf/config.nice diff --git a/php/ext/google/protobuf/php-upb.c b/php/ext/google/protobuf/php-upb.c index 7cdc04293d..6ce85f1e6a 100644 --- a/php/ext/google/protobuf/php-upb.c +++ b/php/ext/google/protobuf/php-upb.c @@ -5665,7 +5665,11 @@ const upb_fielddef *upb_msg_whichoneof(const upb_msg *msg, if (upb_oneof_done(&i)) return false; f = upb_oneof_iter_field(&i); field = upb_fielddef_layout(f); - oneof_case = _upb_getoneofcase_field(msg, field); + if (in_oneof(field)) { + oneof_case = _upb_getoneofcase_field(msg, field); + } else { + return _upb_hasbit_field(msg, field) ? f : NULL; + } return oneof_case ? upb_msgdef_itof(m, oneof_case) : NULL; } @@ -6876,7 +6880,7 @@ static void jsondec_field(jsondec *d, upb_msg *msg, const upb_msgdef *m) { return; } - if (upb_fielddef_containingoneof(f) && + if (upb_fielddef_realcontainingoneof(f) && upb_msg_whichoneof(msg, upb_fielddef_containingoneof(f))) { jsondec_err(d, "More than one field for this oneof."); } diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php index ea8bd65d5c..cea1e6a47f 100644 --- a/php/tests/encode_decode_test.php +++ b/php/tests/encode_decode_test.php @@ -344,6 +344,24 @@ class EncodeDecodeTest extends TestBase $this->assertSame(0, $m2->getTrueOptionalInt32()); } + public function testJsonEncodeDecodeOptional() + { + $m = new TestMessage(); + $this->assertFalse($m->hasTrueOptionalInt32()); + $data = $m->serializeToJsonString(); + $this->assertSame("{}", $data); + + $m->setTrueOptionalInt32(0); + $this->assertTrue($m->hasTrueOptionalInt32()); + $data = $m->serializeToJsonString(); + $this->assertNotSame("{}", $data); + + $m2 = new TestMessage(); + $m2->mergeFromJsonString($data); + $this->assertTrue($m2->hasTrueOptionalInt32()); + $this->assertSame(0, $m2->getTrueOptionalInt32()); + } + public function testJsonEncodeDecodeOneof() { $m = new TestMessage();