OpenAPITools / OpenAPITools/openapi-generator

[BUG][KOTLIN] Kotlin Okhttp4 can't create client when cookie and header has the same name

Open
#19,301 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?
Description

Currently the openapi generator can't generate client for openapi spec endpoint where Header and Cookie is named the same.

openapi-generator version

id("org.openapi.generator") version "7.7.0"

OpenAPI declaration file content or url
---
openapi: "3.0.3"
info:
  description: "test"
  title: "test"
  version: 0.0.1
components:
  schemas:
    token:
      type: "string"
      example: "..."
  parameters:
    tokenHeader:
      name: "token"
      in: "header"
      required: false
      description: "token header"
      schema:
        $ref: "#/components/schemas/token"
    tokenCookie:
      name: "token"
      in: "cookie"
      required: false
      description: "..."
      schema:
        $ref: "#/components/schemas/token"
paths:
  /test:
    post:
      summary: "..."
      parameters:
        - $ref: "#/components/parameters/tokenHeader"
        - $ref: "#/components/parameters/tokenCookie"
      responses:
        204:
          description: "..."
Generation Details

The generated code looks like this

/**
     * To obtain the request config of the operation testPost
     *
     * @param token ...
     * @return RequestConfig
     */
    fun testPostRequestConfig(token: kotlin.String?) : RequestConfig<Unit> {
        val localVariableBody = null
        val localVariableQuery: MultiValueMap = mutableMapOf()
        val localVariableHeaders: MutableMap<String, String> = mutableMapOf()
        
        return RequestConfig(
            method = RequestMethod.POST,
            path = "/test",
            query = localVariableQuery,
            headers = localVariableHeaders,
            requiresAuthentication = false,
            body = localVariableBody
        )
    }

Here we can clearly see that the token argument is not used, this is wrong!

Steps to reproduce

Use the openapi spec to generate the client. I have used gradle and this configuration:

tasks.register("generateRestClient", org.openapitools.generator.gradle.plugin.tasks.GenerateTask::class) {
    group = "openapi tools"
    description = "Generate REST client from OpenAPI specification"
    generatorName.set("kotlin")
    library.set("jvm-okhttp4")
    inputSpec.set("$rootDir/src/main/openapi/openapi.yaml")
    outputDir.set("$buildDir/generated")
    configOptions.put("dateLibrary", "java8") // Configuration option to use Java 8 date/time library
}
Suggest a fix

The generated function should look like this:

/**
     * To obtain the request config of the operation testPost
     *
     * @param token ...
     * @return RequestConfig
     */
    fun testPostRequestConfig(token: kotlin.String?) : RequestConfig<Unit> {
        val localVariableBody = null
        val localVariableQuery: MultiValueMap = mutableMapOf()
        val localVariableHeaders: MutableMap<String, String> = mutableMapOf()
        token?.apply { localVariableHeaders["token"] = this.toString() }
        
        return RequestConfig(
            method = RequestMethod.POST,
            path = "/test",
            query = localVariableQuery,
            headers = localVariableHeaders,
            requiresAuthentication = false,
            body = localVariableBody
        )
    }

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 by generating the provided OpenAPI 3.0.3 specification with the Kotlin generator, using the jvm-okhttp4 library and the Gradle GenerateTask configuration. Inspect the generated testPostRequestConfig function and the generator logic that maps header and cookie parameters. Done means the generated client preserves both same-named parameters correctly and the resulting Kotlin client builds.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.