Proto2: nested types are always optional, required is ignored
See original GitHub issueHi! 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:
- Created a year ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In proto3, message fields are always optional on the wire. That means somebody can send you a
User
message without theorg
field, and it’s perfectly valid. Theorg
property is optional to reflect that. I don’t know whyoptional
is allowed for message fields in proto3, it doesn’t make sense to me and I guess it was an oversight.I missed the original description, let’s keep this open!