Open chuckbenger opened 3 years ago
Please try newer stable version v5.1.0 instead.
Hi, I have the same issue with version 5.4.0.
This is my enum:
title: CashbookEntryType
type: string
nullable: true
enum:
- in
- out
description: Cashbook type.
And here's my model:
title: CashbookEntry
oneOf:
- $ref: ./CashbookEntryIn.yaml
- $ref: ./CashbookEntryOut.yaml
discriminator:
propertyName: type
mapping:
in: ./CashbookEntryIn.yaml
out: ./CashbookEntryOut.yaml
The problem is on the generated constructor:
public CashbookEntry() {
this.type = this.getClass().getSimpleName();
}
This is valid only for String discriminators, not with Enum ones. The correct version for Enum discriminators is the following one, using the fromValue method of the enum type:
public CashbookEntry() {
this.type = CashbookEntryType.fromValue(this.getClass().getSimpleName());
}
For now, I fixed creating a custom template and using this (really ugly, I added a futile loop on vars) fragment to modify the generated constructor method:
{{#discriminator}}
public {{classname}}() {
{{#vars}}
{{#isDiscriminator}}
{{#isString}}
this.{{{discriminatorName}}} = this.getClass().getSimpleName();
{{/isString}}
{{^isString}}
this.{{{discriminatorName}}} = {{{datatypeWithEnum}}}.fromValue(this.getClass().getSimpleName());
{{/isString}}
{{/isDiscriminator}}
{{/vars}}
}
{{/discriminator}}
but it would be way better to add a field to the "discriminator" variable to be able to select the correct instruction to insert in the generated code.
Is this going to be fixed sometime soon?
Description
When using the oneOf expression with enums the code fails to compile because the interface has a function of type "public String getType();", while the children nodes have the implemented method defined as "public TypeEnum getType() {}" which doesn't match the contract. If I manually change the concrete classes to match the interface (ex changing the TypeEnum method to public String getType() { return type.value; } it compiles and works fine.
I'm wondering if I'm doing something wrong or what.
Example Components
Which then generates this
Configuration
openapi-generator version
Openapi-generator-4.3.1