Skip to content

Field.optional should not be !Field.required for proto3 #1850

@egamble

Description

@egamble

protobuf.js version: 7.1.2

We are parsing a .proto file in proto3 format with protobufjs.loadSync. Using the resulting parsed objects, we need to distinguish between a field that is marked optional vs. a field that is not marked optional and also not marked required. Unfortunately, the protobufjs parser always sets Field.optional to the value of !Field.required, so we can't detect the optional keyword in the parsed Field object.

We use the Golang protobuf library with the same .proto file (in proto3 format) which generates different Golang code for fields marked optional, vs. fields not marked as either optional or required. We need to use the protobufjs parser in a similar way.

For example, we would like to detect the difference between the parsed field1 and field2 objects in this .proto file:

syntax = "proto3";

message Message1 {
  string field1 = 1;
  optional string field2 = 2;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions