-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Enums from other packages as path parameters is missing package name in front of enumValMap. #1203
Comments
Nice find! Would you be interested in submitting a fix for this? |
Sure, I'd be happy to, though it might take me a bit of time to get started on it (the earliest I can start is probably next weekend). |
I'm waiting on my company to hear back about the CLA. Meanwhile, my fix is at https://github.com/kwonalbert/grpc-gateway. It was just some small updates to the template for codegen. |
Great work, looking forward to the PR! |
Really nice find, @kwonalbert . I run into this problem in my first attempt to use |
Bump; I have this same issue and would love this merged into the 1.x branch |
This change does not work: kwonalbert@14e42a6 You end up with: e, err = runtime.Enum(val, path/to/specific/package.Table_value)
if err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "table", err)
}
protoReq.Table = path / to / specific / package.Table(e) |
This change worked for me: diff --git a/protoc-gen-grpc-gateway/internal/gengateway/template.go b/protoc-gen-grpc-gateway/internal/gengateway/template.go
index 392110a..22952f1 100644
--- a/protoc-gen-grpc-gateway/internal/gengateway/template.go
+++ b/protoc-gen-grpc-gateway/internal/gengateway/template.go
@@ -359,10 +359,10 @@ var (
{{if $param.IsNestedProto3}}
err = runtime.PopulateFieldFromPath(&protoReq, {{$param | printf "%q"}}, val)
{{if $enum}}
- e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Target.Message.File.GoPkg.Path}}_value)
+ e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Method.Service.File.GoPkg.Path}}_value)
{{end}}
{{else if $enum}}
- e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Target.Message.File.GoPkg.Path}}_value)
+ e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Method.Service.File.GoPkg.Path}}_value)
{{else}}
{{$param.AssignableExpr "protoReq"}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}})
{{end}}
@@ -370,13 +370,13 @@ var (
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", {{$param | printf "%q"}}, err)
}
{{if and $enum $param.IsRepeated}}
- s := make([]{{$enum.GoType $param.Target.Message.File.GoPkg.Path}}, len(es))
+ s := make([]{{$enum.GoType $param.Method.Service.File.GoPkg.Path}}, len(es))
for i, v := range es {
- s[i] = {{$enum.GoType $param.Target.Message.File.GoPkg.Path}}(v)
+ s[i] = {{$enum.GoType $param.Method.Service.File.GoPkg.Path}}(v)
}
{{$param.AssignableExpr "protoReq"}} = s
{{else if $enum}}
- {{$param.AssignableExpr "protoReq"}} = {{$enum.GoType $param.Target.Message.File.GoPkg.Path}}(e)
+ {{$param.AssignableExpr "protoReq"}} = {{$enum.GoType $param.Method.Service.File.GoPkg.Path}}(e)
{{end}}
{{end}}
{{end}}
@@ -521,10 +521,10 @@ func local_request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ct
{{if $param.IsNestedProto3}}
err = runtime.PopulateFieldFromPath(&protoReq, {{$param | printf "%q"}}, val)
{{if $enum}}
- e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Target.Message.File.GoPkg.Path}}_value)
+ e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Method.Service.File.GoPkg.Path}}_value)
{{end}}
{{else if $enum}}
- e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Target.Message.File.GoPkg.Path}}_value)
+ e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Method.Service.File.GoPkg.Path}}_value)
{{else}}
{{$param.AssignableExpr "protoReq"}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.Registry.GetRepeatedPathParamSeparator | printf "%c" | printf "%q"}}{{end}})
{{end}}
@@ -532,13 +532,13 @@ func local_request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ct
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", {{$param | printf "%q"}}, err)
}
{{if and $enum $param.IsRepeated}}
- s := make([]{{$enum.GoType $param.Target.Message.File.GoPkg.Path}}, len(es))
+ s := make([]{{$enum.GoType $param.Method.Service.File.GoPkg.Path}}, len(es))
for i, v := range es {
- s[i] = {{$enum.GoType $param.Target.Message.File.GoPkg.Path}}(v)
+ s[i] = {{$enum.GoType $param.Method.Service.File.GoPkg.Path}}(v)
}
{{$param.AssignableExpr "protoReq"}} = s
{{else if $enum}}
- {{$param.AssignableExpr "protoReq"}} = {{$enum.GoType $param.Target.Message.File.GoPkg.Path}}(e)
+ {{$param.AssignableExpr "protoReq"}} = {{$enum.GoType $param.Method.Service.File.GoPkg.Path}}(e)
{{end}}
{{end}}
{{end}} |
That change seems better than what I was doing. I still haven't heard back about the CLA from my company, so feel free to file a PR with your change for this instead.. |
Thanks Matthew, would you like to contribute a PR with this fix? |
Steps you follow to reproduce the error:
I have two packages
package_a
, andpackage_b
which importspackage_a
.package_b
defines a RPC that uses a message frompackage_a
, and uses an enum from the said message in the path. E.g.,The code that's generated for
package_b
looks like the following:This causes compile time error due to
AEnum_value
andAEnum
being undefined (they are defined inpackage_a
).What did you expect to happen instead:
It should have
package_a.
in front ofAEnum_value
andAEnum
, which seems to make it compile okay.What's your theory on why it isn't working:
It seems that when the code for an enum that is part of a message from another package is generated for path parameters, it's omitting the package name in the front, and assumes the enum is part of the current package.
The text was updated successfully, but these errors were encountered: