OpenAPITools / OpenAPITools/openapi-generator

[BUG][Kotlin] Multiplatform API client uses internal Ktor API

Open
#22,618 1 comment 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?
  • 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

The API client generation for Kotlin Multiplatform uses an internal Ktor API when generating the form body.

In my example, two parameters are Instants. The generated client uses Ktor's FormBuilder#append method. Because the two parameters are objects and not primitives, it uses the generic append method, which is marked as internal API, leading to a compiler error.

    open suspend fun addDocumentFile(documentId: kotlin.String, file: io.ktor.client.request.forms.FormPart<io.ktor.client.request.forms.InputProvider>, fileCreationTime: kotlin.time.Instant? = null, fileLastModified: kotlin.time.Instant? = null): HttpResponse<Unit> {

        val localVariableAuthNames = listOf<String>("Zitadel")

        val localVariableBody = 
            formData {
                documentId?.apply { append("documentId", documentId) }
                file?.apply { append(file) }
                fileCreationTime?.apply { append("fileCreationTime", fileCreationTime) }
                fileLastModified?.apply { append("fileLastModified", fileLastModified) }
            }

        val localVariableQuery = mutableMapOf<String, List<String>>()
        val localVariableHeaders = mutableMapOf<String, String>()

        val localVariableConfig = RequestConfig<kotlin.Any?>(
            RequestMethod.PUT,
            "/document/file/v1",
            query = localVariableQuery,
            headers = localVariableHeaders,
            requiresAuthentication = true,
        )

        return multipartFormRequest(
            localVariableConfig,
            localVariableBody,
            localVariableAuthNames
        ).wrap()
    }

The two Instants would either need to be converted to FormParts before being passed or to primitives. My current workaround is to override the multiplatform api.mustache template file to append a toString() if the type of the parameter is not a string.

With the workaround, it looks as follows. This works because the toString on Instant prints to ISO.

    open suspend fun addDocumentFile(documentId: kotlin.String, file: io.ktor.client.request.forms.FormPart<io.ktor.client.request.forms.InputProvider>, fileCreationTime: kotlin.time.Instant? = null, fileLastModified: kotlin.time.Instant? = null): HttpResponse<Unit> {

        val localVariableAuthNames = listOf<String>("Zitadel")

        val localVariableBody = 
            formData {
                documentId?.apply { append("documentId", documentId) }
                file?.apply { append(file) }
                fileCreationTime?.apply { append("fileCreationTime", fileCreationTime.toString()) }
                fileLastModified?.apply { append("fileLastModified", fileLastModified.toString()) }
            }

        val localVariableQuery = mutableMapOf<String, List<String>>()
        val localVariableHeaders = mutableMapOf<String, String>()

        val localVariableConfig = RequestConfig<kotlin.Any?>(
            RequestMethod.PUT,
            "/document/file/v1",
            query = localVariableQuery,
            headers = localVariableHeaders,
            requiresAuthentication = true,
        )

        return multipartFormRequest(
            localVariableConfig,
            localVariableBody,
            localVariableAuthNames
        ).wrap()
    }
openapi-generator version

7.18.0

OpenAPI declaration file content or url

https://gist.github.com/rlnt/d4576cc399abec0d32f32ac8897b2120

Generation Details

OpenAPI Gradle plugin

openApiGenerate {
    generatorName = "kotlin"
    inputSpec = openApiSpecFile.map { it.asFile.path }
    templateDir = "${rootDir.absolutePath}/.openapi/templates"
    outputDir = layout.buildDirectory.dir("generated/openapi")
        .map { it.asFile.path }
    typeMappings.put(
        "kotlinx.datetime.Instant",
        "kotlin.time.Instant"
    )
    importMappings.put(
        "kotlinx.datetime.Instant",
        "kotlin.time.Instant"
    )
    packageName = "${appId}.openapi"
    generateModelTests = false
    generateApiTests = false
    cleanupOutput = true
    configOptions = mapOf(
        "dateLibrary" to "kotlinx-datetime",
        // "enumPropertyNaming" to "UPPERCASE", currently bugged, has no effect
        "library" to "multiplatform",
        "omitGradlePluginVersions" to "true",
        "omitGradleWrapper" to "true",
        // currently bugged, produces double serializable annotations, automatically set with "library" to "multiplatform"
        // https://github.com/OpenAPITools/openapi-generator/issues/18904
        // "serializationLibrary" to "kotlinx_serialization",
        "sourceFolder" to "commonMain/kotlin"
    )
}
Suggest a fix

The parameters need to be converted to primitives or other supported data types that don't make use of the internal API.

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 Multiplatform api.mustache template and inspect how formData appends non-string parameters such as kotlin.time.Instant. Update the generated form body so these parameters avoid Ktor's internal generic append API, then regenerate the client from the linked OpenAPI declaration and confirm the output compiles.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.