One of our external API uses query param names with special characters (don't ask me why, I don't know). My feign client's method for this API is declared like this:
@GetMapping("/v1/user/{userId}/orders")
List<Order> getOrders(
@PathVariable("userId") String userId,
@RequestParam("page[number]") Integer pageNumber,
@RequestParam("page[size]") Integer pageSize);
As I mentioned, request params contain special characters [
and ]
.
I'm using Spock for testing and I want to set up Wiremock stub like this:
wiremock.stubFor(
get(urlPathMatching('/v1/users/' + userId + '/orders'))
.withQueryParam("page[number]", new EqualToPattern("1"))
.withQueryParam("page[size]", new AnythingPattern())
.willReturn(
status(200)
.withHeader("Content-type", "application/json")
.withBody("""[]""")
))
But I get:
--------------------------------------------------------------------------------------------------
| Closest stub | Request |
--------------------------------------------------------------------------------------------------
|
GET | GET
/v1/users/123/orders | /v1/users/123/orders?page%5Bnumber%5D=%7Bpage%5Bnumber%5
| D%7D&page%5Bsize%5D=%7Bpage%5Bsize%5D%7D
|
Query: page[number] = 1 | <<<<< Query is not present
Query: page[size] [anything] (always) | <<<<< Query is not present
|
--------------------------------------------------------------------------------------------------
After lots of trials and errors, I came up with a solution to use @PathVariable
s instead of @RequestParam
s in feign client method:
@GetMapping("/v1/users/{userId}/orders?page[number]={pageNumber}&page[size]={pageSize}")
List<Order> getOrders(
@PathVariable("userId") String userId,
@PathVariable("pageNumber") Integer pageNumber,
@PathVariable("pageSize") Integer pageSize);
and encode all query params in Wiremock
wiremock.stubFor(
get(urlPathMatching('/v1/users/' + userId + '/orders'))
.withQueryParam("page%5Bnumber%5D", new EqualToPattern("1"))
.withQueryParam("page%5Bsize%5D", new AnythingPattern())
.willReturn(
status(200)
.withHeader("Content-type", "application/json")
.withBody("""[]""")
))
Then it works. But it looks like a kind of a hack. It is problematic to use an optional query params as well.
Is there a way to use @RequestParam
s with special characters?
It looks like a bug in Spring?
In the mean time I will try to debug it to understand where is the problem.