protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Cleanup Request: Clarification Needed for Handling Nested Classes in Protobuf Descriptors

Open ryantzr1 opened this issue 1 year ago • 1 comments

What language does this apply to? This applies to Proto2 and Proto3 syntax.

Describe the problem you are trying to solve. When using nested classes in Protocol Buffers, users must define the type_name field for message or enum types. Additionally, the nestedType field must be included in the descriptor. However, this requirement is not clearly documented, leading to errors like "Field with message or enum type missing type_name".

Describe the solution you'd like The documentation should clearly state that when defining nested messages or enums, users need to:

  • Define the type_name field.
  • Include nestedType in the descriptor to correctly handle the nested structures.

An example should be provided to demonstrate the correct usage.

Additional context Error message encountered: "Field with message or enum type missing type_name".

Here's an example:


// Nested message type
message InnerMessage {
        int32 id = 1;
        string name = 2;
}

message OuterMessage {
    // Field using the nested message type
    InnerMessage inner = 1;
}

Correct Usage in Descriptor When defining the OuterMessage in the descriptor, you must include the type_name for the InnerMessage field and add nestedType to properly link the nested message.

{
  "name": "OuterMessage",
  "field": [
    {
      "name": "inner",
      "number": 1,
      "label": "LABEL_OPTIONAL",
      "type": "TYPE_MESSAGE",
      "type_name": "InnerMessage"
    }
  ],
  "nestedType": [
    {
      "name": "InnerMessage",
      "field": [
        {
          "name": "id",
          "number": 1,
          "label": "LABEL_OPTIONAL",
          "type": "TYPE_INT32"
        },
        {
          "name": "name",
          "number": 2,
          "label": "LABEL_OPTIONAL",
          "type": "TYPE_STRING"
        }
      ]
    }
  ]
}

ryantzr1 avatar Aug 28 '24 09:08 ryantzr1

@ryantzr1 It sounds like you might be constructing descriptors directly. If so, we don't recommend it. Is something going wrong with protoc? The example you provide seems to compile just fine.

googleberg avatar Sep 09 '24 02:09 googleberg

Thank you for the clarification. I understand that the example compiles correctly, and I appreciate your input. However, the issue I'm trying to highlight is more about the documentation itself.

It seems that there isn't clear guidance for users around the need to explicitly define the type_name field and specify nestedType when working with nested messages or enums. While this might be straightforward for experienced users, it could cause confusion for others and lead to errors like the one I encountered: "Field with message or enum type missing type_name."

It would be helpful if the documentation could emphasize the need to:

  1. Define the type_name field for nested message or enum types.
  2. Include the nestedType field in the descriptor to properly handle nested structures.

Including this information would help users better understand how to avoid these issues. Thanks again for your assistance!

ryantzr1 avatar Sep 16 '24 08:09 ryantzr1

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Feb 05 '25 10:02 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar Feb 19 '25 10:02 github-actions[bot]