Skip to content

Commit

Permalink
Add helper function that can prevent errors and reduce log noise (#142)
Browse files Browse the repository at this point in the history
* Add helper function that can prevent errors and reduce log noise

* Fix reference to a file in readme
  • Loading branch information
Calebjh authored and Evgeniy-L committed Nov 16, 2018
1 parent 76fb480 commit 76877a4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
19 changes: 18 additions & 1 deletion gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| ----------|-----------------------------------|---------------------------------------------------- |
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions gateway/filter.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 3 additions & 2 deletions gateway/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down

0 comments on commit 76877a4

Please sign in to comment.