diff --git a/php/src/Google/Protobuf/Internal/Descriptor.php b/php/src/Google/Protobuf/Internal/Descriptor.php index 02b543329011a..a7f80d534e01e 100644 --- a/php/src/Google/Protobuf/Internal/Descriptor.php +++ b/php/src/Google/Protobuf/Internal/Descriptor.php @@ -193,6 +193,10 @@ public static function buildFromProto($proto, $file_proto, $containing) $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 @@ public static function buildFromProto($proto, $file_proto, $containing) $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 875280b75ca66..3a9a73b729ff9 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptor.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptor.php @@ -187,6 +187,11 @@ public function getContainingOneof() 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 @@ private static function isTypePackable($field_type) /** * @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 @@ public static function getFieldDescriptor($proto, $desc) 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 @@ public static function getFieldDescriptor($proto, $desc) $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 @@ public static function getFieldDescriptor($proto, $desc) 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 a74be1e58518a..9c8a8ed7f20b7 100644 --- a/php/src/Google/Protobuf/Internal/OneofDescriptor.php +++ b/php/src/Google/Protobuf/Internal/OneofDescriptor.php @@ -76,10 +76,13 @@ public static function buildFromProto($oneof_proto, $desc, $index) $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 9717da8a77f18..66ffbd5caf9d5 100644 --- a/php/src/Google/Protobuf/OneofDescriptor.php +++ b/php/src/Google/Protobuf/OneofDescriptor.php @@ -63,6 +63,12 @@ public function getName() */ 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]); }