question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proto2: nested types are always optional, required is ignored

See original GitHub issue

Hi! I am working on a system with uses proto2, using protobuf-ts on the web browser side and having an issue where nested types are always generated as optional. Here is an example:

message Test {
  required User user = 1;  
  repeated Role roles = 2;
  required string name = 3;
  optional Settings settings = 4;
}

export interface Test {
    /**
     * @generated from protobuf field: users.User user = 1;
     */
    user?: User;
    /**
     * @generated from protobuf field: repeated users.Role roles = 2;
     */
    roles: Role[];
    /**
     * @generated from protobuf field: string name = 3;
     */
    name: string;
    /**
     * @generated from protobuf field: optional users.Settings settings = 4;
     */
    settings?: Settings;
}

user field shouldn’t be optional in that case, only settings. As you see name is generated correctly. I know support for proto2 is limited but I hope for some help here, it is really, really hard to find a good library for TypeScript with full proto2 support. Protobuf-ts is the closest one from multiple libs I tested… Thanks in advance for any help!

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
timostammcommented, Jul 19, 2022

In proto3, message fields are always optional on the wire. That means somebody can send you a User message without the org field, and it’s perfectly valid. The org property is optional to reflect that. I don’t know why optional is allowed for message fields in proto3, it doesn’t make sense to me and I guess it was an oversight.

0reactions
timostammcommented, Aug 5, 2022

I missed the original description, let’s keep this open!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Language Guide | Protocol Buffers - Google Developers
Defining A Message Type; Scalar Value Types; Optional Fields And Default Values; Enumerations; Using Other Message Types; Nested Types; Updating A Message ...
Read more >
Why required and optional is removed in Protocol Buffers 3
Proto2 allowed a field to be either 'required' or 'optional' and allowed specifying default values, but only for 'optional' fields. The ' ...
Read more >
Issues · timostamm/protobuf-ts - GitHub
Proto2 : nested types are always optional, required is ignored. #340 opened on Jun 27 by dragonnn · 5. Symbol missing from generated...
Read more >
nugget/proto/google/protobuf/descriptor.proto - Google Git
The syntax of the proto file. // The supported values are "proto2" and "proto3". optional string syntax = 12;. } // Describes a...
Read more >
descriptor.proto - GitHub
Proto2 optional fields do not set this flag, because they already indicate ... That class will always contain the .proto file's getDescriptor() method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found