grpc-ecosystem / grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec
https://grpc-ecosystem.github.io/grpc-gateway/
BSD 3-Clause "New" or "Revised" License
18.13k stars 2.24k forks source link

Extend google.protobuf.EnumOptions #2671

Open garthk opened 2 years ago

garthk commented 2 years ago

🚀 Feature

Extend google.protobuf.EnumOptions so we can set examples on enums.

enum State {
  option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_enum) = {
    example: "ACTIVE"
  };
  NONE = 0;
  ACTIVE = 1;
  INACTIVE = 2;
}

Workaround: set the example on every message using the enum.

johanbrandhorst commented 2 years ago

Hm, this sounds reasonable. It would require adding a new extension here: https://github.com/grpc-ecosystem/grpc-gateway/blob/master/protoc-gen-openapiv2/options/annotations.proto, and then weaving through the logic for parsing this value into the generator.

SaumyaBhushan commented 2 years ago

Can you explain a little bit more about how it can be achieved?

johanbrandhorst commented 2 years ago

Hi, thanks for your interest in this issue. We'd need to add a new annotation message, similar to the existing ones in the file I linked above that already exist for other types of annotations. We'd need need to add the logic for parsing this annotation into the generator. I'd recommend just searching through the code to find how we're using one of the existing messages to find the logic that does this. Once we have this information, we can then add it to the enum generation code in the protoc-gen-openapiv2 generator. If you are unfamiliar with the code base, I suggest starting by looking at the protoc-gen-openapiv2 entrypoint and following the logic through from there: https://github.com/grpc-ecosystem/grpc-gateway/blob/master/protoc-gen-openapiv2/main.go

Hope that helps!

LucasDGS commented 2 years ago

does anyone know if this feature has been added?

I need to change the default value of the enums, is there any way to do this?

johanbrandhorst commented 2 years ago

I don't believe that it has been added yet.

MakDon commented 2 years ago

I will work on it these days

jai-50 commented 7 months ago

I would love to use this feature and it appears the PR from 2022 is now stale. Any objection to me trying to get a new PR together for this? The mention shows how we can get examples displayed on enums but doesnt enable setting a common description, title or example to be used across all fields which reference your enum. So it seems like there is still value in this work?

johanbrandhorst commented 7 months ago

Please feel free to start your own PR, thank you!

MakDon commented 7 months ago

I will work on it these days

Sorry for the stale PR and I have closed it. It was too difficult to merge with master at that time as master had changed a lot. I thought I would make it one day but... @jai-50 please feel free to start your own PR. Thanks very much.