OpenAPITools / OpenAPITools/openapi-generator

[BUG][KOTLIN] Request parameters in Kotlin client are not URL encoded

Open
#10,552 0 comments 1 reaction 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?
Description

Path parameters are not URL encoded for the Kotlin generator.

openapi-generator version

5.2.1

OpenAPI declaration file content or url
openapi: '3.0.1'

info:
  contact:
    email: test@test.com
    name: test
    url: http://test.com
  description: test
  license:
    name: "UNLICENSED"
  title: test
  version: test

servers:
  - url: "http://localhost"

paths:
  /test/{parameter}:
    post:
      summary: test
      operationId: test
      parameters:
        - name: parameter
          in: path
          required: true
          schema:
            type: string
          example: test/value
      responses:
        '200':
          description: OK

Generation Details
openapi-generator-cli generate -g kotlin -i temp.yaml
Steps to reproduce

Call the API with

DefaultApi().test("first/second")

which will call the API with POST /test/first/second instead of POST /test/first%2Fsecond.

Similarly, if I encode the parameter manually (DefaultApi().test("first%2Fsecond")), the % is escaped instead.

It seems the entire path is encoded as a whole instead of each parameter.

Related issues/PRs
Suggest a fix

Specifically the generated code

return RequestConfig(
    method = RequestMethod.POST,
    path = "/test/{parameter}".replace("{"+"parameter"+"}", "$parameter"),
    query = localVariableQuery,
    headers = localVariableHeaders,
    body = localVariableBody
)

is the problem. The parameter is replaced as is, while it should be URL encoded.

URL encoding of path parameters should be added for each parameter, not for the path as a whole.

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 generated Kotlin DefaultApi request construction shown in the issue and reproduce it using the provided OpenAPI YAML, generation command, and test call. Trace how the path parameter is substituted and verify that a value such as "first/second" becomes "first%2Fsecond" without double-encoding an already encoded value. Done means generated Kotlin clients URL-encode each path parameter correctly.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.