OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][SPRING] Problem with URI encoding with RestTemplate in ApiClient (version 5.4.0)

Open
#14,782 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Hi, in my projects I use the 5.4.0 version of the plugin. I have a problem when the path variables have special characters like the spaces.

In particular, I recieve this Exception:

Caused by: java.net.URISyntaxException: Illegal character in path at index 109

The bug is caused by ApiClient for RestTemplate, in this point (https://github.com/OpenAPITools/openapi-generator/blob/v5.4.0/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache#L625-L632):

public String expandPath(String pathTemplate, Map<String, Object> variables) {
    // disable default URL encoding
    DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory();
    uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
    final RestTemplate restTemplate = new RestTemplate();
    restTemplate.setUriTemplateHandler(uriBuilderFactory);
        
}

That piece of code invalids the encoding mode of RestTemplate passed of client (and the default encoding mode).

The master branch fixed it (https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache#L610-L612):

public String expandPath(String pathTemplate, Map<String, Object> variables) {
        return restTemplate.getUriTemplateHandler().expand(pathTemplate, variables).toString();
}

but I can't update the plugin from version 5 to version 6 (for too many impacts). Is it possible a 5.4.1 version for this fix?
(I woudl like to not create a custom APiClient template to fix it).

openapi-generator version

Version 5.4.0

Steps to reproduce

For example, with this path:

String requestId = "PREPARE_ANALOG_DOMICILE.  OBJECT_HAWE-DAYD-LVRK-202302-G-1.INDEX_0.SENTATTEMPTMADE_0";
 String path = "/try-channel-private/v1/b2b/try-deliveries-prepare/{requestId}"
                .replace("{requestId}", requestId);

I received the Exception: Caused by: java.net.URISyntaxException: Illegal character in path...

Suggest a fix

The fix like in master branch.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache at the expandPath implementation referenced in the issue, and compare it with the master-branch version linked there. Reproduce the failure using the supplied requestId containing spaces; done means RestTemplate's configured URI encoding is preserved and the path no longer raises URISyntaxException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.