OpenAPITools / OpenAPITools/openapi-generator
[BUG] Discriminator classes don't implement parent interface (in Kotlin generator)
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
openapi-generator version
7.10.0
OpenAPI declaration file content or url
components:
schemas:
Car.CommandInput.jsonld-Input:
oneOf:
- $ref: '#/components/schemas/DrivingModeSetCommandInput.jsonld-Input'
- $ref: '#/components/schemas/PowerModeSetCommandInput.jsonld-Input'
...
- $ref: '#/components/schemas/TemperatureDownCommandInput.jsonld-Input'
discriminator:
propertyName: command
mapping:
driving_mode_set: '#/components/schemas/DrivingModeSetCommandInput.jsonld-Input'
power_mode_set: '#/components/schemas/PowerModeSetCommandInput.jsonld-Input'
...
temperature_down: '#/components/schemas/TemperatureDownCommandInput.jsonld-Input'
DrivingModeSetCommandInput.jsonld-Input:
type: object
description: ''
deprecated: false
required:
- command
properties:
command:
default: driving_mode_set
example: driving_mode_set
type: string
enum:
- driving_mode_set
commandOptions:
type: object
properties:
drivingMode:
type: string
enum:
- eco
- comfort
- performance
- outing
- snow
- individual
additionalProperties: false
required:
- drivingMode
PowerModeSetCommandInput.jsonld-Input:
type: object
description: ''
deprecated: false
required:
- command
properties:
command:
default: power_mode_set
example: power_mode_set
type: string
enum:
- power_mode_set
commandOptions:
type: object
properties:
powerMode:
type: string
enum:
- intelligent
- electric
- fuel
- save_mode
additionalProperties: false
required:
- powerMode
...
TemperatureDownCommandInput.jsonld-Input:
type: object
description: ''
deprecated: false
required:
- command
properties:
command:
default: temperature_down
example: temperature_down
type: string
enum:
- temperature_down
commandOptions:
type: object
properties:
step:
type: number
format: float
minimum: 1
maximum: 14
multipleOf: 0.5
additionalProperties: false
required:
- step
default:
step: 1
example:
step: 1
Generation Details
# tried with and without --openapi-normalizer REF_AS_PARENT_IN_ALLOF=true
openapi-generator-cli generate -i openapi.json -g kotlin -c config.yaml --global-property apiTests=false,apiDocs=false,modelTests=false,modelDocs=false -o output
# config.yaml
additionalProperties:
library: multiplatform
dateLibrary: kotlinx-datetime
sourceFolder: src/main/kotlin
useCoroutines: true
Steps to reproduce
command above generates parent interface + children data classes, but it doesn't implements this interface. also for some reasons parent interface has some default values from its last child (see commends in code below):
// CarCommandInputjsonldInput.kt
/**
* // <---why do these empy lines needed?
*
* @param command
* @param commandOptions
*/
// <---why do these empy lines needed?
interface CarCommandInputjsonldInput {
@SerialName(value = "command") @Required val command: CarCommandInputjsonldInput.Command
@SerialName(value = "commandOptions") val commandOptions: TemperatureDownCommandInputJsonldInputCommandOptions? // <----- why last child type is used here???
/**
* // <---why do these empy lines needed?
*
* Values: temperature_down
*/
@Serializable
enum class Command(val value: kotlin.String) {
@SerialName(value = "temperature_down") temperature_down("temperature_down"); // <----- why last child type is used here???
}
}
// DrivingModeSetCommandInputjsonldInput.kt
/**
*
*
* @param command
* @param commandOptions
*/
@Serializable
data class DrivingModeSetCommandInputjsonldInput (
@SerialName(value = "command") @Required val command: DrivingModeSetCommandInputjsonldInput.Command = Command.driving_mode_set,
@SerialName(value = "commandOptions") val commandOptions: DrivingModeSetCommandInputJsonldInputCommandOptions? = null
) {
/**
*
*
* Values: driving_mode_set
*/
@Serializable
enum class Command(val value: kotlin.String) {
@SerialName(value = "driving_mode_set") driving_mode_set("driving_mode_set");
}
}
// PowerModeSetCommandInputjsonldInput.kt
/**
*
*
* @param command
* @param commandOptions
*/
@Serializable
data class PowerModeSetCommandInputjsonldInput (
@SerialName(value = "command") @Required val command: PowerModeSetCommandInputjsonldInput.Command = Command.power_mode_set,
@SerialName(value = "commandOptions") val commandOptions: PowerModeSetCommandInputJsonldInputCommandOptions? = null
) {
/**
*
*
* Values: power_mode_set
*/
@Serializable
enum class Command(val value: kotlin.String) {
@SerialName(value = "power_mode_set") power_mode_set("power_mode_set");
}
}
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/19928
Suggest a fix
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 running the supplied OpenAPI declaration with the shown Kotlin generator command and inspect CarCommandInputjsonldInput.kt plus the generated discriminator child data classes. Compare the parent interface with each child, focusing on interface implementation and the inherited commandOptions and enum values. Done means generated children implement the parent interface and the parent no longer uses the last child's types or values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100