Adding decodeSlash using Feign @RequestLine
Asked Answered
S

3

2

I am currently using a YAML file to generate the models and the API clients using the swagger plugin and I am using Feign OkHttpClient to make requests to the API, the problem here is the client encodes the URL but ignores the Slash(es) with this the API call fails. Is there a way to add decodeSlash parameter in the client? Or can this be achieved using an interceptor?

Here is the sample path param where I am running into this issue. QgKuK2DU/0%3D where as it should be QgKuK2DU%2F0%3D

Scarf answered 31/10, 2018 at 19:7 Comment(0)
S
0

decodeSlash can only be set via the @RequestLine annotation. If you do not have access to the annotation you will need to replace the uri using a RequestInterceptor.

Shingly answered 15/12, 2018 at 1:33 Comment(1)
Where does the @RequestLine need to be added exactly ?Slump
D
0

If you use the openapi-generator you can modify the templates (also described here) yourself to add the decodeSlash Parameter:

git clone https://github.com/openapitools/openapi-generator
cd openapi-generator
git checkout v4.2.0 # The Version Tag you are actually using
cd modules/openapi-generator/src/main/resources/Java/libraries/feign/
cp api.mustache <your_local_project>/src/main/resources/Java/libraries/feign

In api.mustache change the 2 appearances of @RequestLine:

- @RequestLine("{{httpMethod}} {{{path}}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}")
+ @RequestLine(value="{{httpMethod}} {{{path}}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}", decodeSlash = false)

- @RequestLine("{{httpMethod}} {{{path}}}?{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}")
+ @RequestLine(value="{{httpMethod}} {{{path}}}?{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{#hasMore}}&{{/hasMore}}{{/queryParams}}", decodeSlash = false)

Using the openapi-generator-maven-plugin add the templateDirectory to the <configuration> block:

<templateDirectory>src/main/resources/Java/libraries/feign</templateDirectory>
Depict answered 5/11, 2019 at 13:29 Comment(0)
S
0

You can configure a contract in the Feign client interface and turn of the decode slash:

@FeignClient(name = "yourClient", url= "xxxx", configuration = YourClient.ClientConf.class)
public interface YourClient {
  class ClientConf{
     @Bean
     Contract contract(@Autowired(required = false) List<AnnotatedParameterProcessor> parameterProcessors, ConversionService feignConversionService) {
         if (parameterProcessors == null) {
             parameterProcessors = new ArrayList<>();
         }
         return new SpringMvcContract(parameterProcessors, feignConversionService, false);
     }
  }
}
Slump answered 15/2 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.