OpenAPITools / OpenAPITools/openapi-generator

[BUG] Kotlin generator Import Mappings Not working as expected

Open
#5,632 2 comments 2 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

Description

I’m using the Maven plugin (version 4.2.3) to generate Kotlin DTOs.
I’m having trouble with the Import Mappings. If I change the generators name to Java then the import mappings happen as I expect but with Kotlin I experience weird behaviour.
Here is my config:

<configuration>
    <inputSpec>
        ${sourceDirectory}/definitions/api_common_1.0.yml
    </inputSpec>
    <output>${sourceDirectory}</output>
    <generatorName>kotlin</generatorName>
    <modelPackage>com.example.infrastructure.adapters.foo.dto</modelPackage>
    <configOptions>
        <useBeanValidation>true</useBeanValidation>
        <dateLibrary>java8</dateLibrary>
        <hideGenerationTimestamp>true</hideGenerationTimestamp>
        <serializationLibrary>gson</serializationLibrary>
    </configOptions>
    <generateApis>false</generateApis>
    <generateApiTests>false</generateApiTests>
    <generateApiDocumentation>false</generateApiDocumentation>
    <generateModelDocumentation>false</generateModelDocumentation>
    <generateModels>true</generateModels>
    <generateSupportingFiles>false</generateSupportingFiles>
    <skipValidateSpec>true</skipValidateSpec>
    <importMappings>
        Status=com.example.infrastructure.adapters.foo.dto.common.v1.Status
    </importMappings>
</configuration>

And here is the Kotlin code that are generated, look at the import of Status:

package com.example.infrastructure.adapters.foo.dto

import com.example.infrastructure.adapters.foo.dto.com.example.infrastructure.adapters.foo.dto.common.v1.Status
import com.google.gson.annotations.SerializedName
/**
 * 
 * @param header 
 */
data class Response (
    @SerializedName("status")
    val status: com.example.infrastructure.adapters.foo.dto.common.v1.Status? = null
) 

The expected output is:

package com.example.infrastructure.adapters.foo.dto

import com.google.gson.annotations.SerializedName
/**
 * 
 * @param header 
 */
data class Response (
    @SerializedName("status")
    val status: com.example.infrastructure.adapters.foo.dto.common.v1.Status? = null
) 
openapi-generator version

4.2.3

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's handling of the supplied importMappings configuration and compare it with the Java generator behavior described in the issue. Reproduce the Maven plugin 4.2.3 configuration, then verify that the generated Kotlin file imports Status without duplicating the model package prefix.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.