OpenAPITools / OpenAPITools/openapi-generator
[BUG][KOTLIN] Wrong type for enums defined in parent when using inheritance
Nobody has claimed this yet.
- 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?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When using inheritance, the child objects in the generated code point to the wrong enum in the child object but it's defined in the parent.
openapi-generator version
Maven plugin versions tested: 5.4.0, 4.3.1, and 6.2.1 (Latest)
OpenAPI declaration file content or URL
Pay attention to the relation and the enum defined in the parent:
"ChildDto" : {
"allOf" : [ {
"$ref" : "#/definitions/ParentDto"
},
{
"swagger" : "2.0",
"info" : {
"description" : "Service",
"version" : "0.0.1",
"title" : "service"
},
"basePath" : "/service",
"tags" : [ {
"name" : "service"
} ],
"paths" : {
},
"securityDefinitions" : {
"basicAuth" : {
"type" : "basic"
}
},
"definitions" : {
"ChildDto" : {
"allOf" : [ {
"$ref" : "#/definitions/ParentDto"
}, {
"type" : "object",
"properties" : {
"title" : {
"type" : "string"
},
"description" : {
"type" : "string"
},
"newsfeedType" : {
"type" : "string",
"enum" : [ "CLASSIC", "CAPTIONED_IMAGE", "BANNER" ]
},
"pinned" : {
"type" : "boolean"
},
"imageUrl" : {
"type" : "string"
},
"iosUri" : {
"type" : "string"
},
"androidUri" : {
"type" : "string"
},
"uriText" : {
"type" : "string"
}
}
} ]
},
"ParentDto" : {
"type" : "object",
"discriminator" : "type",
"properties" : {
"schedule" : {
"type" : "integer",
"format" : "int64"
},
"type" : {
"type" : "string",
"enum" : [ "PUSH", "NEWSFEED" ]
}
}
}
}
}
Generation Details
Maven configuration:
<execution>
<id>testservice</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.build.directory}/swagger/test.json</inputSpec>
<generateApis>false</generateApis>
<modelPackage>com.test.model</modelPackage>
<generatorName>kotlin-spring</generatorName>
<skipValidateSpec>true</skipValidateSpec>
<configOptions>
<enumPropertyNaming>UPPERCASE</enumPropertyNaming>
<useTags>true</useTags>
<interfaceOnly>true</interfaceOnly>
<library>spring-boot</library>
<reactive>true</reactive>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
</execution>
Steps to reproduce
Copy the input file (Swagger definition) into the directory defined in the inputSpec and Run mvn clean compile
The generated code is:
ChildDto:
data class ChildDto(
@field:JsonProperty("title") val title: kotlin.String? = null,
@field:JsonProperty("description") val description: kotlin.String? = null,
@field:JsonProperty("newsfeedType") val newsfeedType: ChildDto.NewsfeedType? = null,
@field:JsonProperty("pinned") val pinned: kotlin.Boolean? = null,
@field:JsonProperty("imageUrl") val imageUrl: kotlin.String? = null,
@field:JsonProperty("iosUri") val iosUri: kotlin.String? = null,
@field:JsonProperty("androidUri") val androidUri: kotlin.String? = null,
@field:JsonProperty("uriText") val uriText: kotlin.String? = null,
@field:JsonProperty("schedule") override val schedule: kotlin.Long? = null,
@field:JsonProperty("type") override val type: ChildDto.Type? = null
) : ParentDto{
/**
*
* Values: CLASSIC,CAPTIONED_IMAGE,BANNER
*/
enum class NewsfeedType(val value: kotlin.String) {
@JsonProperty("CLASSIC") CLASSIC("CLASSIC"),
@JsonProperty("CAPTIONED_IMAGE") CAPTIONED_IMAGE("CAPTIONED_IMAGE"),
@JsonProperty("BANNER") BANNER("BANNER");
}
}
ParentDto:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes(
JsonSubTypes.Type(value = ChildDto::class, name = "ChildDto")
)
interface ParentDto{
val schedule: kotlin.Long?
val type: ParentDto.Type?
/**
*
* Values: PUSH,NEWSFEED
*/
enum class Type(val value: kotlin.String) {
@JsonProperty("PUSH") PUSH("PUSH"),
@JsonProperty("NEWSFEED") NEWSFEED("NEWSFEED");
}
}
And you can find a compilation error in the ChildDto::type field because references to a ChildDto.Type type that doesn't exist because that type is defined in the parent, so here is the wrong one:
@field:JsonProperty("type") override val type: ChildDto.Type? = null, // <== COMPILATION ERROR: ChildDto.Type don't found
Expected generated code:
@field:JsonProperty("type") override val type: ParentDto.Type? = null,
Related issues/PRs
Suggest a fix
Use the right reference to the class that contains the types when using inheritance. ChildDto.Type >> ParentDto.Type
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the issue with the supplied Swagger definition and the kotlin-spring Maven configuration using mvn clean compile. Compare the generated ChildDto and ParentDto declarations, especially the inherited type property. Done means the generated child property references ParentDto.Type and the generated project compiles successfully.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100