From 0f1574aae26613b19e55c8812694fda3d79814ae Mon Sep 17 00:00:00 2001 From: "Thanet (Knack) Praneenararat" Date: Thu, 9 Jun 2022 16:53:27 +0900 Subject: [PATCH] Fixed bugs of OneofDescriptor. --- .../Google/Protobuf/Internal/Descriptor.php | 9 ++++---- .../Protobuf/Internal/FieldDescriptor.php | 23 ++++++++----------- .../Protobuf/Internal/OneofDescriptor.php | 2 ++ php/src/Google/Protobuf/OneofDescriptor.php | 6 +++++ 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/php/src/Google/Protobuf/Internal/Descriptor.php b/php/src/Google/Protobuf/Internal/Descriptor.php index 02b543329..a7f80d534 100644 --- a/php/src/Google/Protobuf/Internal/Descriptor.php +++ b/php/src/Google/Protobuf/Internal/Descriptor.php @@ -193,6 +193,10 @@ class Descriptor $desc->setLegacyClass($legacy_classname); $desc->setOptions($proto->getOptions()); + foreach ($proto->getField() as $field_proto) { + $desc->addField(FieldDescriptor::buildFromProto($field_proto)); + } + // Handle nested types. foreach ($proto->getNestedType() as $nested_proto) { $desc->addNestedType(Descriptor::buildFromProto( @@ -213,11 +217,6 @@ class Descriptor $index++; } - // Pass the descriptor to build FieldDescriptor after the OneofDescriptors are populated. - foreach ($proto->getField() as $field_proto) { - $desc->addField(FieldDescriptor::buildFromProto($field_proto, $desc)); - } - return $desc; } } diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptor.php b/php/src/Google/Protobuf/Internal/FieldDescriptor.php index 875280b75..3a9a73b72 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptor.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptor.php @@ -187,6 +187,11 @@ class FieldDescriptor return $this->containing_oneof; } + public function setContainingOneof($containing_oneof) + { + $this->containing_oneof = $containing_oneof; + } + public function getRealContainingOneof() { return !is_null($this->containing_oneof) && !$this->containing_oneof->isSynthetic() @@ -240,10 +245,9 @@ class FieldDescriptor /** * @param FieldDescriptorProto $proto - * @param Descriptor $desc * @return FieldDescriptor */ - public static function getFieldDescriptor($proto, $desc) + public static function getFieldDescriptor($proto) { $type_name = null; $type = $proto->getType(); @@ -257,13 +261,7 @@ class FieldDescriptor break; } - if ($proto->hasOneofIndex()) { - $oneof_index = $proto->getOneofIndex(); - $containing_oneof = $desc->getOneofDecl()[$oneof_index]; - } else { - $oneof_index = -1; - $containing_oneof = null; - } + $oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1; // TODO: once proto2 is supported, this default should be false // for proto2. if ($proto->getLabel() === GPBLabel::REPEATED && @@ -282,10 +280,7 @@ class FieldDescriptor $field = new FieldDescriptor(); $field->setName($proto->getName()); - $field->containing_oneof = $containing_oneof; - $json_name = $proto->hasJsonName() ? $proto->getJsonName() : - lcfirst(implode('', array_map('ucwords', explode('_', $proto->getName())))); if ($proto->hasJsonName()) { $json_name = $proto->getJsonName(); } else { @@ -324,8 +319,8 @@ class FieldDescriptor return $field; } - public static function buildFromProto($proto, $parent_desc) + public static function buildFromProto($proto) { - return FieldDescriptor::getFieldDescriptor($proto, $parent_desc); + return FieldDescriptor::getFieldDescriptor($proto); } } diff --git a/php/src/Google/Protobuf/Internal/OneofDescriptor.php b/php/src/Google/Protobuf/Internal/OneofDescriptor.php index a74be1e58..432368571 100644 --- a/php/src/Google/Protobuf/Internal/OneofDescriptor.php +++ b/php/src/Google/Protobuf/Internal/OneofDescriptor.php @@ -76,8 +76,10 @@ class OneofDescriptor $oneof = new OneofDescriptor(); $oneof->setName($oneof_proto->getName()); foreach ($desc->getField() as $field) { + /** @var FieldDescriptor $field */ if ($field->getOneofIndex() == $index) { $oneof->addField($field); + $field->setContainingOneof($oneof); } } return $oneof; diff --git a/php/src/Google/Protobuf/OneofDescriptor.php b/php/src/Google/Protobuf/OneofDescriptor.php index 9717da8a7..66ffbd5ca 100644 --- a/php/src/Google/Protobuf/OneofDescriptor.php +++ b/php/src/Google/Protobuf/OneofDescriptor.php @@ -63,6 +63,12 @@ class OneofDescriptor */ public function getField($index) { + if ( + is_null($this->internal_desc->getFields()) + || !isset($this->internal_desc->getFields()[$index]) + ) { + return null; + } return $this->getPublicDescriptor($this->internal_desc->getFields()[$index]); }