Skip to content

[JAVA][Client][RestTemplate] Query parameters not properly URL-encoded #644

@rubms

Description

@rubms
Description

Before #260 (fix for #249), query parameters were not URL encoded.

#260 attempts to fix this problem, but after #260 was applied a problem still persists: URLs are not correctly URL-encoded. The % characters of the encoded URL get re-encoded. For instance: the query parameter date=2018-07-25T00:00:00+02:00, which should be URL-encoded as date=2018-07-25T00%3A00%3A00%2B02%3A00 is actually encoded as date=2018-07-25T00%253A00%253A00%252B02%253A00.

This way, the server interprets the query parameter as 2018-07-25T00%3A00%3A00%2B02%3A00 instead of 2018-07-25T00:00:00+02:00.

openapi-generator version

3.1.2-SNAPSHOT (build 20180724.111212-30)

OpenAPI declaration file content or url
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: soldAfter
          in: query
          description: If specified, only pets sold after the given date and time will be returned.
          required: false
          schema:
            type: string
            format: date-time
Command line used for generation
java -jar openapi-generator-cli-3.1.2-20180724.111212-30.jar generate -g java -i .\sample.yml -c .\config.json -o test_output

config.json:

{
	"library": "resttemplate",
	"dateLibrary": "java8"
}
Steps to reproduce

Auto-generate the code from the swagger specification. Perform an HTTP GET request from the auto-generated code including a date-time as query parameter. The URL of the HTTP request will be incorrectly URL-encoded, having a double encoding of the % characters.

Related issues/PRs

The #249 issue originally addresses the problem in the URL-encoding of query parameters in the Java RestTemplate client generation. A fix is attempted in #260, but without full success, in which this re-encoding of % characters is experienced.

Suggest a fix/enhancement

The problem lies in the invokeAPI of the auto-generated ApiClient class:

        final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
        if (queryParams != null) {
            //encode the query parameters in case they contain unsafe characters
            for (List<String> values : queryParams.values()) {
                if (values != null) {
                    for (int i = 0; i < values.size(); i++) {
                        try {
                            values.set(i, URLEncoder.encode(values.get(i), "utf8"));
                        } catch (UnsupportedEncodingException e) {

                        }
                    }
                }
            }
            builder.queryParams(queryParams);
        }
        
        final BodyBuilder requestBuilder = RequestEntity.method(method, builder.build().toUri());

The query parameters are explicitly encoded in the for loop, but the builder.build().toUri() piece of code is escaping the % characters in the URL!

Building the URL like this would fix the problem:

        final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
        if (queryParams != null) {
            //encode the query parameters in case they contain unsafe characters
            for (List<String> values : queryParams.values()) {
                if (values != null) {
                    for (int i = 0; i < values.size(); i++) {
                        try {
                            values.set(i, URLEncoder.encode(values.get(i), "utf8"));
                        } catch (UnsupportedEncodingException e) {

                        }
                    }
                }
            }
            builder.queryParams(queryParams);
        }

        URI uri;
        try {
            uri = new URI(builder.build().toUriString());
        } catch(URISyntaxException ex)  {
            throw new RestClientException("Could not build URL: " + builder.toUriString(), ex);
        }

        final BodyBuilder requestBuilder = RequestEntity.method(method, uri);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions