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

Add example of using ref to reference unused messages for openapi.json inclusion #5241

Open
protiumx opened this issue Feb 14, 2025 · 5 comments

Comments

@protiumx
Copy link

🚀 Feature

When defining generic responses for different status codes, I use an unbound message as follows

message GenericResponse {
  repeated string resources = 1;
  repeated string errors = 2;
}

option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
  responses: {
    key: "400"
    value: {
      description: "Returned when the request is malformed."
      schema: {
        json_schema: {ref: "#/definitions/GenericResponse"}
      }
    }
  }
}

Because the message GenericResponse is not bound to any rpc, the swagger definition is not generated.
The workaround is to generate a No-op service and rpc that use the message

service ExampleService {
  rpc Create(google.protobuf.Empty) returns (GenericResponse);
}

Request

Allow for the generation of unbounded messages via configuration. Similar to the generate_unbound_methods flag.

@johanbrandhorst
Copy link
Collaborator

Hi, thanks for the issue. I don't think this should need a new flag, I would expect us to be able to detect these top level definitions and use that to include the message. Does it work if you tie this response to a specific RPC instead? I think we had to do something similar for the gRPC Status message type.

@protiumx
Copy link
Author

Hi @johanbrandhorst
Correct, at the moment I can get the message schema generated by binding it to a no-op RPC, which I ought to replace with this feature request. In the Schema field for the top level options, I don't see a way I could bind/include the message, I only see json_schema: {ref: "#/definitions/GenericResponse"}

@johanbrandhorst
Copy link
Collaborator

So as it happens we have an example of this working when defined globally as in your example:

As you can see, ErrorResponse isn't used in any rpcs in this protobuf but it's still being rendered. Could you try to reproduce the problem and compare it to the one in a_bit_of_everything.proto?

@protiumx
Copy link
Author

Amazing, using the schema.$ref works for my use case. Thanks @johanbrandhorst .
It could be worth to add a note in the docs as this is only visible in the examples, but I consider this feature request resolved.

@johanbrandhorst
Copy link
Collaborator

Great idea, would you be willing to contribute a little section to the docs? The files are in https://github.com/grpc-ecosystem/grpc-gateway/tree/main/docs/docs.

@johanbrandhorst johanbrandhorst changed the title [Feature] Generate type annotations for unbound messages Add example of using ref to reference unused messages for openapi.json inclusion Feb 22, 2025
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

2 participants