spring-cloud-openfeign icon indicating copy to clipboard operation
spring-cloud-openfeign copied to clipboard

FeignClient not sending proper form url encoded request

Open techestop4u opened this issue 3 years ago • 2 comments
trafficstars

We have a scenario where a POST application/form-url-encoded request is to be sent to an API endpoint. And we are using @FeignClient annotated interface having a @PostMapping annotated function which takes in a POJO class as an argument such that the attributes of the class will be set as key value pairs of form data.

The issue is that the POJO class ( which is an argument to the function ) is derived from a base class. Due to which the form data posted to the API endpoint does not contain the attributes of the base class.

It works fine if the argument to @PostMapping function is a simple POJO class which doesn't extend any other class.

Can you please suggest a solution to the issue?

techestop4u avatar Mar 24 '22 17:03 techestop4u

I have just created an example repository to reproduce the problem: https://github.com/JavierSA/issue-spring_cloud_openfeign-form_url_encoded

JavierSA avatar Apr 06 '22 13:04 JavierSA

Thanks @JavierSA, will take a look tomorrow.

OlgaMaciaszek avatar Jul 25 '22 14:07 OlgaMaciaszek

Hi, @OlgaMaciaszek. Do you have any news about this regression bug?

I have updated the README of https://github.com/JavierSA/issue-spring_cloud_openfeign-form_url_encoded with the new tested versions of Spring Boot and Spring Cloud OpenFeign.

JavierSA avatar Feb 22 '23 15:02 JavierSA

Hi @techestop4u @JavierSA , sorry for not getting back to you earlier. @JavierSA, thanks for providing the sample. I was able to reproduce the issue. What we did is switch to feign-form, which is the dedicated form handling library for Feign, for URL-encoded requests. You should create the issue related to the fact that superclass fields are not being evaluated in that repo. Since it seems this issue does not affect a whole lot of users, I don't think we would like to add fallbacks specific to this scenario in SC OpenFeign at this point, however, I'm going to suggest a workaround that you could use.

Since Encoder beans have @ConditionalOnMissingBean on them, you should be able to substitute them with custom Encoder beans instantiated in your Feign Client Configuration. You could just override the SpringEncoder's encode method by one that has the following excerpt removed to fall back to the way it was being processed previously.

if (isFormRelatedContentType(requestContentType)) {
	springFormEncoder.encode(requestBody, bodyType, request);
	return;
}

OlgaMaciaszek avatar Feb 24 '23 15:02 OlgaMaciaszek

Hi, @OlgaMaciaszek. Thank you so much for your workaround!

I have overridden the method encode so, if it isFormUrlEncoded, then it is encodeWithMessageConverter as happened with SC OpenFeign 3.0.3, instead of being encoded with SpringFormEncoder, which is what happens with versions >= 3.0.4.

It is not the best code I've written 😅 (e.g., encodeWithMessageConverter is private), but it works. 🙂

  • 3.0.3 behaviour: https://github.com/spring-cloud/spring-cloud-openfeign/blob/v3.0.3/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java#L98
  • 3.0.4 behaviour: https://github.com/spring-cloud/spring-cloud-openfeign/blob/v3.0.4/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringEncoder.java#L104

JavierSA avatar Mar 01 '23 10:03 JavierSA

@JavierSA I'm happy the workaround works for you. I'd still suggest you create an issue with Feign-Form so that they properly address it.

OlgaMaciaszek avatar Mar 01 '23 11:03 OlgaMaciaszek