Skip to content

Commit

Permalink
Export defaultQueryParser struct for custom query parsers (grpc-eco…
Browse files Browse the repository at this point in the history
…system#2651)

* Export `defaultQueryParser` struct for custom query parsers

* Update v2 migration guide for query parameter parsing change
  • Loading branch information
mikesouza authored Apr 17, 2022
1 parent dc093b5 commit 2ce32af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/docs/development/grpc-gateway_v2_migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,7 @@ There is no workaround for this, as we considered it a correct interpretation of
## Error handling configuration has been overhauled

`runtime.HTTPError`, `runtime.OtherErrorHandler`, `runtime.GlobalHTTPErrorHandler`, `runtime.WithProtoErrorHandler` are all gone. Error handling is rewritten around the use of gRPCs Status types. If you wish to configure how the gateway handles errors, please use `runtime.WithErrorHandler` and `runtime.WithStreamErrorHandler`. To handle routing errors (similar to the removed `runtime.OtherErrorHandler`) please use `runtime.WithRoutingErrorHandler`.

## Default query parameter parsing behaviour change

The default behaviour for query parameter parsing has changed to return an `InvalidArgument` (`400 Bad Request`) error when more than one of the same matching query parameters is parsed. Previously, it would log but not return an error, using the first query parameter that matched and ignoring any others. See [the original issue](https://github.com/grpc-ecosystem/grpc-gateway/issues/2632) for more information.
10 changes: 7 additions & 3 deletions runtime/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

var valuesKeyRegexp = regexp.MustCompile(`^(.*)\[(.*)\]$`)

var currentQueryParser QueryParameterParser = &defaultQueryParser{}
var currentQueryParser QueryParameterParser = &DefaultQueryParser{}

// QueryParameterParser defines interface for all query parameter parsers
type QueryParameterParser interface {
Expand All @@ -37,11 +37,15 @@ func PopulateQueryParameters(msg proto.Message, values url.Values, filter *utili
return currentQueryParser.Parse(msg, values, filter)
}

type defaultQueryParser struct{}
// DefaultQueryParser is a QueryParameterParser which implements the default
// query parameters parsing behavior.
//
// See https://github.com/grpc-ecosystem/grpc-gateway/issues/2632 for more context.
type DefaultQueryParser struct{}

// Parse populates "values" into "msg".
// A value is ignored if its key starts with one of the elements in "filter".
func (*defaultQueryParser) Parse(msg proto.Message, values url.Values, filter *utilities.DoubleArray) error {
func (*DefaultQueryParser) Parse(msg proto.Message, values url.Values, filter *utilities.DoubleArray) error {
for key, values := range values {
match := valuesKeyRegexp.FindStringSubmatch(key)
if len(match) == 3 {
Expand Down

0 comments on commit 2ce32af

Please sign in to comment.