Sterling Smith
Sterling Smith

Reputation: 51

Is there a way to prevent Spring Cloud Gateway from reordering query parameters?

Spring Cloud Gateway appears to be reordering my query parameters to put duplicate parameters together.

I'm trying to route some requests to one of our end points to a third party system. These requests include some query parameters that need to be in a specific order (including some duplicate parameters), or the third party system returns a 500 error, but upon receiving the initial request with the parameters in the proper order, the Spring Cloud Gateway reorders these parameters to put the duplicates together by the first instance of the parameter.

Example:

http://some-url.com/a/path/here?foo=bar&anotherParam=paramValue2&aThirdParam=paramValue3&foo=bar

Becomes:

http://some-url.com/a/path/here?foo=bar&foo=bar&anotherParam=paramValue2&aThirdParam=paramValue3

Where the last parameter was moved to be by the first parameter because they had the same name.

The actual request output I need is for the query parameters to be passed through without change.

Upvotes: 1

Views: 2233

Answers (1)

TYsewyn
TYsewyn

Reputation: 622

The issue lays in the UriComponentsBuilder which is used in RouteToRequestFilter. UriComponentsBuilder.fromUri(uri) is going to build up a map of query params. Because this is a LinkedMultiValueMap you see the reordering of the used query params.

Note that RFC3986 contains the following

The query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource within the scope of the URI’s scheme and naming authority (if any).

Therefor I don’t think there needs to be a fix in Spring Cloud Gateway.

In order to fix this in your gateway, you'll need to add a custom filter which kicks in after the RouteToRequestFilter by setting the order to RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER + 1. Take a look at the RouteToRequestUrlFilter how the exchange is adapted to go to the downstream URI.

Hope that helps! :)

Upvotes: 1

Related Questions