[BUG] [java] wrong enum default value
Description
WIth openapi-generator version 7.12.0 there was a new feature to set a default value for enum values. This will end in a compile error for our code generation.
Version: 7.12.0
{
"components": {
"schemas": {
"ContactMedium": {
"properties": {
"@type": {
"type": "string",
"description": "When sub-classing, this defines the sub-class entity name",
"enum": [
"EmailContactMedium",
"PhoneContactMedium",
"WebsiteContactMedium",
"SocialNetworkContactMedium",
"PostalAddressContactMedium",
"BulkRecipientPostalAddressContactMedium",
"PostboxContactMedium"
]
}
}
}
}
}
}
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
....
<configuration>
<generatorName>jaxrs-spec</generatorName>
<configOptions>
<libary>quarkus</libary>
<enumPropertyNaming>legacy</enumPropertyNaming>
<ignoreAnyOfInEnum>true</ignoreAnyOfInEnum>
<useJakartaEe>true</useJakartaEe>
<generatePom>false</generatePom>
<interfaceOnly>true</interfaceOnly>
<returnResponse>true</returnResponse>
<containerDefaultToNull>true</containerDefaultToNull>
</configOptions>
<generateSupportingFiles>false</generateSupportingFiles>
<generateApiTests>false</generateApiTests>
<ignoreFileOverride>${project.basedir}/../.openapi-generator-ignore-quarkus</ignoreFileOverride>
</configuration>
</execution>
</executions>
</plugin>
(same for spring - java)
This will generate the following line in class ContactMedium:
private AtTypeEnum atType = ContactMedium;
Wich will result in a Compile Error on class ContactMedium:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.14.0:compile (default-compile) on project tmf632-quarkus3: Compilation failure
[ERROR] .../ContactMedium.java:[130,31] Symbol nicht gefunden
[ERROR] Symbol: Variable ContactMedium
[ERROR] Ort: Klasse ....ContactMedium
@PaKu5 What do you mean by " there was a new feature to set a default value for enum values"?
When the default value is in the list of enumeration, DefaultCodegen uses toEnumDefaultValue()
public String toEnumDefaultValue(String value, String datatype) {
return datatype + "." + value;
}
Otherwise the default value is used as is.
Have you set the default value to "default": "ContactMedium" ?
With version 7.11.0 there was no default value for the generated Code.
It was generated like:
private AtTypeEnum atType;
With version 7.12.0 the value "ContactMedium" was assigned and this will end in an compile error.
No I didn't set any default value for the property.
Not reproducable.
I've tried with java -jar openapi-generator-cli-7.12.0.jar generate -g jaxrs-spec -o out -i enum.json --additional-properties=library=quarkus,enumPropertyNaming=legacy
Please provide as requested by bug reports:
Prefer CLI steps, including the language, libraries and various options.
Sorry, I forgot some necassary parts of the api-spec.
{
"openapi": "3.0.1",
"components": {
"schemas": {
"ContactMedium": {
"required": [
"@type"
],
"x-class-extra-annotation": "@org.bson.codecs.pojo.annotations.BsonDiscriminator(key=\"atType\")",
"properties": {
"@type": {
"type": "string",
"description": "When sub-classing, this defines the sub-class entity name",
"enum": [
"EmailContactMedium",
"PhoneContactMedium",
"WebsiteContactMedium",
"SocialNetworkContactMedium",
"PostalAddressContactMedium",
"BulkRecipientPostalAddressContactMedium",
"PostboxContactMedium"
]
}
},
"discriminator": {
"propertyName": "@type",
"mapping": {
"ContactMedium": "#/components/schemas/ContactMedium",
"EmailContactMedium": "#/components/schemas/EmailContactMedium",
"PhoneContactMedium": "#/components/schemas/PhoneContactMedium",
"WebsiteContactMedium": "#/components/schemas/WebsiteContactMedium",
"SocialNetworkContactMedium": "#/components/schemas/SocialNetworkContactMedium",
"PostalContactMedium": "#/components/schemas/PostalContactMedium",
"PostalAddressContactMedium": "#/components/schemas/PostalAddressContactMedium",
"BulkRecipientPostalAddressContactMedium": "#/components/schemas/BulkRecipientPostalAddressContactMedium",
"PostboxContactMedium": "#/components/schemas/PostboxContactMedium"
}
}
}
}
}
}
Now the problem is in line 80 of the generated code (ContactMedium).
The subtypes exists in the original api-spec but this makes no difference.
So the issue is a discriminator issue.
You have a discriminator mapping
"ContactMedium": "#/components/schemas/ContactMedium", using a value "ContactMedium" that is not in the list of enumerations.
So your contract is invalid.
So either add it to the ilst of enumerations:
"enum": [
"ContactMedium",
-->
private AtTypeEnum atType = AtTypeEnum.CONTACTMEDIUM;
or remove the line
"ContactMedium": "#/components/schemas/ContactMedium"
->
private AtTypeEnum atType;
Thats correct, I fixed the API-Spec and everything works fine now.
Thank you for your support.