swagger-api / swagger-api/swagger-codegen

[JAVA][RestTemplate] Client: Plus character in query parameter not encoded

Open
#7,747 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I have the problem that in the generated client (language Java, library resttemplate) when I pass a plus character within a string query parameter then the client will send the plus unescaped to the server which interprets it as space. See also my StackOverflow question.

Swagger-codegen version

CLI version 2.3.1

Swagger declaration file content or url
swagger: '2.0'
info:
  title: Test for Issue
  version: v1
host: 'localhost:8080'
schemes:
  - http
basePath: /
tags:
  - name: test
paths:
  /test:
    get:
      tags:
        - test
      operationId: testMe
      produces:
        - application/json
      parameters:
        - name: filter
          in: query
          type: string
      responses:
        '200':
          description: OK
Command line used for generation

From my gradle script task:

args = ['generate',
            '-i', "${projectDir}/src/main/resources/swagger.yaml",
            '--api-package', 'com.example.demo.client.api',
            '--model-package', 'com.example.demo.client.model',
            '--invoker-package', 'com.example.demo.client',
            '--additional-properties', 'dateLibrary=java8',
            '--remove-operation-id-prefix',
            '--lang', 'java',
            '--library', 'resttemplate',
            '-o', "${buildDir}/generatedJava"]
Steps to reproduce

I made a minimal Spring boot 2.0 example with gradle including client generation:
demo.zip

To execute client generation start task "generateRestClient_sync". Call task "test" to start the REST server and the test will invoke the REST api via the generated client (see test class). Once the REST controller method is called, it prints the query parameter in log. There you can see that it replaced the + by space.

Related issues/PRs

none

Suggest a fix/enhancement

I assume that this is a bug of Spring's RestTemplate itself because it does escape all other characters but the plus sign. In my StackOveflow question, one answerer recommended changing the template but in my opinion it should be fixed not just for me. When I use the generated client I expect to not take care of any encoding issues. I can try to propose a fix once you confirm this is a bug.

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 with the minimal Spring Boot demo and the generated Java RestTemplate client described in the issue; run the generateRestClient_sync and test tasks to reproduce the plus character becoming a space. Trace the generated query-parameter handling and confirm completion when a plus in the filter value reaches the server as an encoded plus character.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.