Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if the message has an optional field with a return value, the proto will attach a same name field with _ prefix. why? #2754

Open
wang-xiaowu opened this issue May 22, 2024 · 4 comments

Comments

@wang-xiaowu
Copy link

Problem description

if the message has an option field with a return value, the proto will attach a same name field with _ prefix. why?

Reproduction steps

  • message like this
message Res {
    uint32 code = 1;
    optional string msg = 2;
    string status = 3;
    optional string error = 4;
    optional uint32 count = 5;
}
  • server retun
    image

  • real result client got
    image

Environment

  • OS name, version and architecture: windows11
  • Node version : 16.15
  • Node installation method. binary
  • If applicable, compiler version [e.g. clang 3.8.0-2ubuntu4]
  • Package name and version: 1.10.8

Additional context

@murgatroid99
Copy link
Member

This is controlled by the message decoding code on the client. What protobuf implementation are you using on the client?

@wang-xiaowu
Copy link
Author

proto3 with @grpc/proto-loader.
this is protoLoaderOptions,

const protoLoaderOptions = {
  keepCase: true,
  longs: String,
  enums: String,
  defaults: true,
  oneofs: true,
};

@wang-xiaowu wang-xiaowu changed the title if the message has an option field with a return value, the proto will attach a same name field with _ prefix. why? if the message has an optional field with a return value, the proto will attach a same name field with _ prefix. why? May 27, 2024
@MasterOdin
Copy link
Contributor

We got bit by this in our application recently where we were passing the response from a gRPC response through camelcase-keys, where when we got something like { msg: 'foo', _msg: 'msg' }, the camelcase version was { msg: 'msg' }.

While we can modify our usage of camelcase-keys, it'd be nice if the loader could just not have these _ fields as we don't view them as useful at all.

For reference, we're also using @grpc/proto-loader with the same settings as above.

@murgatroid99
Copy link
Member

OK, I see what's happening here: Protobuf.js internally represents optional fields as a single-member oneof group named as the underscore-prefixed field name. The plain object message representation includes the oneof group as a separate key to indicate which member is set because you set the oneofs option to true.

You can disable this behavior by setting the oneofs option to false.

This behavior can only be changed in Protobuf.js, so I have filed an issue there: protobufjs/protobuf.js#2037.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants