For someone not familiar with Feign: Feign is a declarative REST client. Meaning, it helps to write clients for REST APIs in concise and quite easy way. More info can be found here.
Some time ago I have faced quite a weird and no-intuitive issue while using the client layer written using Spring Cloud wrapper around Feign.
In this post I want to share the lesson learned with you.
For the sake of simplicity I will demonstrate the issue using the one GET method client.
So, we have a client defined like this:
@FeignClient("http://self",url = "http://localhost:8090")
interface JustClient {
@GetMapping("/simpleEndpoint?test=true")
fun getSimpleObject(@RequestParam("myValue") value: String): SimpleObject
}
When we call the getSimpleObject
method it forms the GET request to http://localhost:8090/simpleEndpoint
and adds to request parameters test=true
and myValue
= the string we have provided as a value parameter to
this method.
Looking at the code it seems that nothing here can go wrong on the client side.
This client can be used as this in our services:
justClient.getSimpleObject("Some string")
Imagine my confusion when on one of the requests the exception like this was thrown:
java.lang.IllegalArgumentException: name is required.
u0009at feign.template.QueryTemplate.create(QueryTemplate.java:66)
u0009at feign.RequestTemplate.lambda$appendQuery$0(RequestTemplate.java:611)
What the name is required
may mean?
Some quick research in Google has led me to this issue. By the time I came to it, there were no valuable comments at all, so the only thing left was the investigation of the data that would have caused the exception.
Some further research has shown, that a few data entries used to generate the request contained special
symbols like &
and =
. Nevertheless, my first thought was that the data passed to a Feign client should be automatically
url-encoded by Feign itself, so this special symbols should cause no problem, and Feing indeed has been encoding the params passed.
Imagine my amazement when the deeper debugging shown that &&
was the source of the error.
It turned out that Feign has treated &&
as a valid delimiter for GET params and was looking for some value in between of two &&
.
After all, the fix for the issue was a manual url-encoding of Feign client method params and still it was a big surprise for me to find such an issue in battle-tested open-source library.
The issue originally was found in the 2.2.1.RELEASE
version of spring-cloud-openfeing
library which uses 10.4.0
version of OpenFeign
inside.
The issue is fixed in the next releases.
The full code reproducing the issue can be found here on GitHub.