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

Repeated fields in parameter are set as a single string v.s. a slice of string (incompatible with grpc-gateway) #1160

Closed
hyang74 opened this issue Oct 20, 2022 · 9 comments · Fixed by #1161
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@hyang74
Copy link

hyang74 commented Oct 20, 2022

I am using this client generator to generate clients that can talk to my server via REST/GRPC in an easy approach. And I am using grpc-gateway for routing the HTTP/REST requests to GRPC. I thought both this and grpc-gateway are popular tools and they all generate code from protobuf so that they should be compatible with each other.
However I noticed that the repeated field parameter didn't work well.

Could you please help to check if this is a bug/issue, or if this is a feature that is nice to have?
I think people would be more than happy if the generated REST Clients can be directly used against grpc-gateway!

Thanks!

Environment details

  • Programming language: Golang
  • OS: MacOS
  • Language runtime version: v1.17.13
  • Package version:

Steps to reproduce

My proto is similar to this:

rpc List(ListRequest) returns (ListResponse) {
    option (google.api.http) = {
      get: "/v1/{parent=*}/lists"
    };
}

message ListRequest {
    enum Types {
        A=0;
        B=1;
    }
    repeated Types = 1; 
}

So I can send a request with Types = [0, 1].

And in the generated REST client's List method, the Types is set in this way

		if req.GetTypes() != nil {
			params.Add("types", fmt.Sprintf("%v", req.GetTypes()))
		}

This sets a single string [0,1] to the parameter types.

Whereas in the server side, grpc-gateway receives the request and parses the parameter using this method: Parse, where the input values is the request.Form which contains the parameter k-v pair.

And it will parse the above types: "[0,1]" to key: types, value: "[0,1]" (Note, it is a string "[0,1]"). And it will fail saying that [0,1] is not a correct enum type.

My solution is to adjust the above code in the generated REST clients:

		if req.GetTypes() != nil {
			for _,n := range req.GetTypes(){
				params.Add("types", fmt.Sprintf("%v", n))
			}
		}

to add multiple strings for the types and this will work perfectly with grpc-gateway.
This will set the parameter fields with a slice of strings 0, 1 and grpc-gateway will parse and return correct values.

@hyang74 hyang74 added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Oct 20, 2022
@hyang74 hyang74 changed the title Repeated fields in parameter is set as a single string vs a slice of string [grpc-gateway] Repeated fields in parameter are set as a single string v.s. a slice of string (incompatible with grpc-gateway) Oct 20, 2022
@noahdietz
Copy link
Collaborator

Thanks for the bug report. I think you are right and your suggested fix to the generated code seems like it would work. I will see about fixing this soon.

@noahdietz
Copy link
Collaborator

This is just a note:
We probably want to change the code for handling repeated fields in the QP here:

if field.GetLabel() == fieldLabelRepeated {
// It's a slice, so check for nil
p("if req%s != nil {", accessor)
.

The paramAdd content is something like params.Add(... so we'd want to either modify that to include the for loop @HuairuoYang suggested, or generate that for loop in place there. Probably the former considering this is what we do for proto-JSON well known types.

@noahdietz
Copy link
Collaborator

Relevant gRPC-HTTP Transcoding semantics that the generator is currently violating are documented in the following comments on the google.api.http message:

This is the standard for the behavior that @HuairuoYang sees in grpc-gateway and that we need to comply with in this code generator.

@noahdietz noahdietz self-assigned this Oct 20, 2022
@hyang74
Copy link
Author

hyang74 commented Oct 20, 2022

Thank you @noahdietz ! Could I ask when would this be fixed and which will it be included in the next release (when)?

@noahdietz
Copy link
Collaborator

Thank you @noahdietz ! Could I ask when would this be fixed and which will it be included in the next release (when)?

Just opened a PR :) If merged today, I can release today. Otherwise, it will go out on Monday (we try not to release on Friday).

@hyang74
Copy link
Author

hyang74 commented Oct 20, 2022

Impressive! Thanks!

@noahdietz
Copy link
Collaborator

Release to come on Monday @HuairuoYang

@noahdietz
Copy link
Collaborator

Should be available in v0.33.2: #1162

@hyang74
Copy link
Author

hyang74 commented Oct 26, 2022

Verified that it works! Thanks again for fast response and execution of the team! @noahdietz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants