From 76877a49bda29a96f6e35c482b23d4ff3e05f5c6 Mon Sep 17 00:00:00 2001 From: Caleb Horst Date: Fri, 16 Nov 2018 02:35:45 -0800 Subject: [PATCH] Add helper function that can prevent errors and reduce log noise (#142) * Add helper function that can prevent errors and reduce log noise * Fix reference to a file in readme --- gateway/README.md | 19 ++++++++++++++++++- gateway/filter.go | 27 +++++++++++++++++++++++++++ gateway/response.go | 5 +++-- 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 gateway/filter.go diff --git a/gateway/README.md b/gateway/README.md index bfcec3da..e806e041 100644 --- a/gateway/README.md +++ b/gateway/README.md @@ -29,7 +29,7 @@ message GetMessageRequest { string user_id = 2; } ``` -This enables the following two alternative HTTP JSON to RPC mappings: +This enables the following two alternative HTTP JSON to RPC mappings: | HTTP Verb | REST Endpoint | RPC | | ----------|-----------------------------------|---------------------------------------------------- | @@ -287,6 +287,23 @@ Response with results and service-defined results tag `rpz_hits` } ``` +## Query String Filtering +When using the collection operators with the grpc-gateway, extraneous errors may +be logged during rpcs as the query string is parsed that look like this: +``` +field not found in *foo.ListFoobarRequest: _order_by +``` +and the usage of any of the collection operator field names without the leading +underscore (`order_by`, `filter`,... instead of `_order_by`, `filter`,...) in +query strings may result in the error `unsupported field type reflect.Value`, +being returned. + +This can be resolved by overwriting the default filter for each rpc with these +operators using the one defined in [filter.go](filter.go). +```golang +filter_Foobar_List_0 = gateway.DefaultQueryFilter +``` + ## Errors ### Format diff --git a/gateway/filter.go b/gateway/filter.go new file mode 100644 index 00000000..28b128f4 --- /dev/null +++ b/gateway/filter.go @@ -0,0 +1,27 @@ +package gateway + +import "github.com/grpc-ecosystem/grpc-gateway/utilities" + +// DefaultQueryFilter can be set to override the filter_{service}_{rpc}_{num} +// field in generated .pb.gw.go files to prevent parse errors in the gateway +// and potentially reduce log noise due to unrecognized fields +var DefaultQueryFilter = utilities.NewDoubleArray(defaultFilterFields) + +var defaultFilterFields = [][]string{ + // collection ops and the expected names used for the collection ops objects in requests + {"paging"}, {limitQueryKey}, {offsetQueryKey}, {pageTokenQueryKey}, + {"order_by"}, {sortQueryKey}, + {"fields"}, {fieldsQueryKey}, + {"filter"}, {filterQueryKey}, +} + +// QueryFilterWith will add extra fields to the standard fields in the default +// filter. +func QueryFilterWith(extraFields []string) *utilities.DoubleArray { + qf := make([][]string, len(defaultFilterFields)+len(extraFields)) + copy(qf, defaultFilterFields) + for _, f := range extraFields { + qf = append(qf, []string{f}) + } + return utilities.NewDoubleArray(qf) +} diff --git a/gateway/response.go b/gateway/response.go index 9754c49f..79d83cfe 100644 --- a/gateway/response.go +++ b/gateway/response.go @@ -4,11 +4,12 @@ import ( "context" "encoding/json" "fmt" + "io" + "net/http" + "github.com/golang/protobuf/proto" "google.golang.org/grpc/codes" "google.golang.org/grpc/grpclog" - "io" - "net/http" "github.com/grpc-ecosystem/grpc-gateway/runtime" )