grpc / grpc-web

gRPC for Web Clients
https://grpc.io
Apache License 2.0
8.46k stars 762 forks source link

Missing messageOptions in generated protos #1323

Closed cognmbs closed 7 months ago

cognmbs commented 1 year ago

Hi, we have a proto definition like this:

syntax = "proto3";

package mypackage;

import "google/protobuf/descriptor.proto";

message MyMessage {
  string my_field = 1 [(my_option) = "value"];
}

extend google.protobuf.FieldOptions {
  string my_option = 1234;
}

But I cannot access my_option in the resulting javascript files:

const messageOptions = MyMessage.messageOptions;

It seems that messageOptions field is missing in the generated code.

sampajano commented 1 year ago

@cognmbs Hi thanks for the report!

I'm actually not too familiar with proto extensions but I tried adding the same option into our echo.proto, and saw the following new code got generated:

/**
 * A tuple of {field number, class constructor} for the extension
 * field named `myOption`.
 * @type {!jspb.ExtensionFieldInfo<string>}
 */
proto.grpc.gateway.testing.myOption = new jspb.ExtensionFieldInfo(
    1234,
    {myOption: 0},
    null,
     /** @type {?function((boolean|undefined),!jspb.Message=): !Object} */ (
         null),
    0);

google_protobuf_descriptor_pb.FieldOptions.extensionsBinary[1234] = new jspb.ExtensionFieldBinaryInfo(
    proto.grpc.gateway.testing.myOption,
    jspb.BinaryReader.prototype.readString,
    jspb.BinaryWriter.prototype.writeString,
    undefined,
    undefined,
    false);
// This registers the extension field with the extended class, so that
// toObject() will function correctly.
google_protobuf_descriptor_pb.FieldOptions.extensions[1234] = proto.grpc.gateway.testing.myOption;

But I cannot access my_option in the resulting javascript files:

const messageOptions = MyMessage.messageOptions;

It seems that messageOptions field is missing in the generated code.

I haven't used FieldOptions myself but according to this page https://protobuf.dev/programming-guides/proto/#customoptions, the syntax (in Java) looks like below:

String value = MyProtoFile.MyMessage.getDescriptor().getOptions()
  .getExtension(MyProtoFile.myOption);

Have you tried something like that?

I'm not sure if you can access MyMessage.messageOptions like you mentioned.

cognmbs commented 1 year ago

@sampajano thanks for your reply. But I found that may be this is an issue of protobuf-javascript: https://github.com/protocolbuffers/protobuf-javascript/issues/85. May be there is another way to achive what we want? Our goal is just to specify proto's field validation rules in the proto and then translate it to JavaScript to avoid duplicating validation logic. Example:

We have a proto named Animal with the field Name.
I want to set the max lenght of this field to be 255 chars and pass this somehow to JavaScript.

Any ideas?

sampajano commented 1 year ago

@cognmbs Ah thanks for finding the protobuf issue!

I'm sorry i'm really not familiar with the feature you're looking for, and would appreciate if anyone is able to provide ideas / suggestions here :)

sampajano commented 7 months ago

I'm closing this due to inactivity. Feel free to reopen if needed :)