OpenAPITools / OpenAPITools/openapi-generator

[BUG][KOTLIN][CLIENT] An openapi get method doesn't honour the "content" type(s) specified in the contract

Open
#19,251 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
  • [Y] Have you provided a full/minimal spec to reproduce the issue?
  • [Y ] Have you validated the input using an OpenAPI validator (example)?
  • [N] Have you tested with the latest master to confirm the issue still exists?
  • [Y ] 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

Given the supplied openapi v.3.0.1 spec file, the kotlin generator would generate the function:

    /**
     * To obtain the request config of the operation downloadFileGet
     *
     * @param fileId 
     * @return RequestConfig
     */
    fun downloadFileGetRequestConfig(fileId: kotlin.String) : RequestConfig<Unit> {
        val localVariableBody = null
        val localVariableQuery: MultiValueMap = mutableMapOf<kotlin.String, kotlin.collections.List<kotlin.String>>()
            .apply {
                put("fileId", listOf(fileId.toString()))
            }
        val localVariableHeaders: MutableMap<String, String> = mutableMapOf()
        localVariableHeaders["Accept"] = "application/json"

        return RequestConfig(
            method = RequestMethod.GET,
            path = "/download-file",
            query = localVariableQuery,
            headers = localVariableHeaders,
            requiresAuthentication = false,
            body = localVariableBody
        )
    }

where the Accept header is set to application/json irrespective of the spec requirements.

Also, I can't find any easy workaround: one could "tweak" the RequestConfig returned by the above function, but then in the generated API I can't find:

  • any downloadFileGet*(...) method which would accept a RequestConfig instance;
  • a way to invoke ApiClient.request(...) directly, since that method is protected and the generated api class isn't open.
openapi-generator version

org.openapi.generator v.7.6.0 gradle plugin

OpenAPI declaration file content or url

full-minimal-spec.json

Generation Details
openApiGenerate {
    generatorName = 'kotlin'
    inputSpec = sourceSwaggerFile.toString()
    outputDir = "${layout.buildDirectory.get()}/generated"
    packageName = android.namespace
    apiPackage = "${android.namespace}.api"
    modelPackage = "${android.namespace}.model"
    configOptions.with {
        library = 'jvm-okhttp4'
        dateLibrary = 'java8'
        moshiCodeGen = 'true'
    }

    apiFilesConstrainedTo.add 'ExampleApi'
    modelFilesConstrainedTo.add ''
    supportingFilesConstrainedTo.add ''
}
Steps to reproduce

Generate the kotlin client

Related issues/PRs

Not found.

Suggest a fix

The best fix: construct an Accept header value which reflects the ones declared in the contract.

The fastest workaround: let the generated API class be open, in order to allow overriding it in user code.

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 Kotlin generator using the supplied full-minimal-spec.json and the shown Gradle configuration, then inspect how the generated downloadFileGetRequestConfig method derives its Accept header. Confirm the generated header against the contract's declared content types and add or update a regression test for the generated client. Done means the generated Kotlin client reflects the contract rather than always using application/json.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
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.