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

Passing ENUM value as URL parameter throws error #166

Closed
igateno opened this issue May 23, 2016 · 3 comments
Closed

Passing ENUM value as URL parameter throws error #166

igateno opened this issue May 23, 2016 · 3 comments

Comments

@igateno
Copy link

igateno commented May 23, 2016

I'm having issues passing an ENUM values as a URL parameter.

For example, this works just fine: /v0/tickets?id=1

But I have if I have an enum Priority like this:

  enum Priority {
    UNDEFINED = 0;
    LOW = 1;
    MEDIUM = 2;
    HIGH = 3;
    CRITICAL = 4;
  }

Neither /v0/tickets?priority=HIGH or /v0/tickets?priority=3 work for me.

The former returns:

400 Bad Request

{
  "Error": "strconv.ParseInt: parsing \"HIGH\": invalid syntax",
  "Code": 3
}

And the latter crashes my gateway server with this stacktrace:

Server listening on port 9090
2016/05/23 11:19:56 http: panic serving [::1]:50880: reflect.Set: value of type int32 is not assignable to type workflow.Ticket_Priority
goroutine 24 [running]:
net/http.(*conn).serve.func1(0xc82016a280)
        /usr/local/go/src/net/http/server.go:1389 +0xc1
panic(0x408820, 0xc820064ca0)
        /usr/local/go/src/runtime/panic.go:426 +0x4e9
reflect.Value.assignTo(0x4045a0, 0xc8200699c0, 0x85, 0x5a9d30, 0xb, 0x4e9540, 0x0, 0x0, 0x0, 0x0)
        /usr/local/go/src/reflect/value.go:2164 +0x3be
reflect.Value.Set(0x4e9540, 0xc8201e00f0, 0x185, 0x4045a0, 0xc8200699c0, 0x85)
        /usr/local/go/src/reflect/value.go:1334 +0x95
github.com/gengo/grpc-gateway/runtime.populateField(0x4e9540, 0xc8201e00f0, 0x185, 0xc8200696a1, 0x1, 0x0, 0x0)
        /Users/isaac/dev/gopath/src/github.com/gengo/grpc-gateway/runtime/query.go:124 +0x468
github.com/gengo/grpc-gateway/runtime.populateFieldValueFromPath(0x997a10, 0xc8200649d0, 0xc8200787a0, 0x2, 0x2, 0xc8200649f0, 0x1, 0x1, 0x0, 0x0)
        /Users/isaac/dev/gopath/src/github.com/gengo/grpc-gateway/runtime/query.go:83 +0x913
github.com/gengo/grpc-gateway/runtime.PopulateQueryParameters(0x997a10, 0xc8200649d0, 0xc8200697a0, 0x84c200, 0x0, 0x0)
        /Users/isaac/dev/gopath/src/github.com/gengo/grpc-gateway/runtime/query.go:22 +0x207
github.com/my-awesome-api/workflow.request_Workflow_ListTickets_0(0x997bd0, 0xc82006ce80, 0x997c10, 0x8412a0, 0x997d70, 0xc82002e218, 0xc82017e0e0, 0xc820069740, 0x0, 0x0, ...)
        /Users/isaac/dev/gopath/src/github.com/my-awesome-api/workflow/workflow.pb.gw.go:38 +0x100
github.com/my-awesome-api/workflow.RegisterWorkflowHandler.func1(0xe04630, 0xc82006b450, 0xc82017e0e0, 0xc820069740)
        /Users/isaac/dev/gopath/src/github.com/my-awesome-api/workflow/workflow.pb.gw.go:404 +0x28c
github.com/gengo/grpc-gateway/runtime.(*ServeMux).ServeHTTP(0xc820184d50, 0xe04630, 0xc82006b450, 0xc82017e0e0)
        /Users/isaac/dev/gopath/src/github.com/gengo/grpc-gateway/runtime/mux.go:89 +0xc52
net/http.serverHandler.ServeHTTP(0xc820166d00, 0xe04630, 0xc82006b450, 0xc82017e0e0)
        /usr/local/go/src/net/http/server.go:2081 +0x19e
net/http.(*conn).serve(0xc82016a280)
        /usr/local/go/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2137 +0x44e
@igateno
Copy link
Author

igateno commented May 23, 2016

I guess I didn't read far enough into the README before posting this.

Any general guidance for someone who might try to dive in and implement the solution?

@buckhx
Copy link

buckhx commented Jul 6, 2016

You'd want to add a method that accepts an enum def to the runtime package in convert.go https://github.com/gengo/grpc-gateway/blob/master/runtime/convert.go

and add it to the proto3ConvertFuncs (& proto2) map https://github.com/gengo/grpc-gateway/blob/master/protoc-gen-grpc-gateway/descriptor/types.go#L293

which gets used by Parameter.ConvertFuncExpr() https://github.com/gengo/grpc-gateway/blob/master/protoc-gen-grpc-gateway/descriptor/types.go#L190

and is eventually rendered here in the template https://github.com/gengo/grpc-gateway/blob/master/protoc-gen-grpc-gateway/gengateway/template.go#L210

@tzneal
Copy link
Contributor

tzneal commented Feb 17, 2017

The below change at least enables /v0/tickets?priority=3, though there's no range checking. I'll submit a PR if it's an acceptable interim solution until someone really fixes it.

diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/query.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/query.go
index 56a919a..a9102b5 100644
--- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/query.go
+++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/query.go
@@ -121,7 +121,7 @@ func populateField(f reflect.Value, value string) error {
        if err := result[1].Interface(); err != nil {
                return err.(error)
        }
-       f.Set(result[0])
+       f.Set(result[0].Convert(f.Type()))
        return nil
 }

@tmc tmc closed this as completed in a0ee7de Feb 17, 2017
@tamalsaha tamalsaha mentioned this issue Mar 30, 2017
1 task
adasari pushed a commit to adasari/grpc-gateway that referenced this issue Apr 9, 2020
- parses enumeration strings and integer values
- range checks both the string and integer form

Fixes grpc-ecosystem#166
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

No branches or pull requests

3 participants