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

Support array-type response body by default #2809

Closed
armsnyder opened this issue Jul 22, 2022 · 3 comments · Fixed by #2813
Closed

Support array-type response body by default #2809

armsnyder opened this issue Jul 22, 2022 · 3 comments · Fixed by #2813

Comments

@armsnyder
Copy link
Contributor

armsnyder commented Jul 22, 2022

🚀 Feature

EDIT: I was able to resolve this by finding the allow_repeated_fields_in_body=true plugin option. However, I suggest changing that option default from false to true.

I would like the openapiv2 and grpc-gateway plugins to support mapping an HTTP response to a repeated field, so that we can have array-type response bodies.

For example, I want my response body to look like:

[{"id":1},{"id":2}]

Instead of:

{"pets":[{"id":1},{"id":2}]}

Example proto:

import "google/api/http.proto";

service PetService {
  rpc GetPets(GetPetsRequest) returns (GetPetsResponse) {
    option (google.api.http) = {
      get: "/pets"
      response_body: "pets"
    };
  }
}

message GetPetsRequest {}

message GetPetsResponse {
  repeated Pet pets = 1;
}

message Pet {
  uint64 id = 1;
}

Plugin output:

Failure: plugin openapiv2: repeated field not allowed in field path: pets in pets
Failure: plugin grpc-gateway: repeated field not allowed in field path: pets in pets

I read the previous discussion in #317; however there are situations where you need an array-type response body, like when you are implementing an existing REST API spec while migrating a codebase to gRPC.

The documentation for the google.api.http is clear that mapping a repeated field to response_body can be supported.

If an API needs to use a JSON array for request or response body, it can map
the request or response body to a repeated field. However, some gRPC
Transcoding implementations may not support this feature.

Thank you 👐

@armsnyder
Copy link
Contributor Author

I just found the allow_repeated_fields_in_body=true plugin option on both plugins, so this resolves my issue. I suggest changing the default value from false to true, since it is not a breaking change. It adds a capability that otherwise causes the plugin to fail.

@armsnyder armsnyder changed the title Support array-type response body Support array-type response body by default Jul 22, 2022
@johanbrandhorst
Copy link
Collaborator

Hi Adam. This is an interesting point, and I agree that it would be nice to turn this on by default. Would you be willing to contribute a flip of this option from off-by-default to on-by-default?

@armsnyder
Copy link
Contributor Author

I'll take a look. 😄

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

Successfully merging a pull request may close this issue.

2 participants