Skip to content

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

@rlnt

Description

@rlnt

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions