Skip to content

[BUG] [java] wrong enum default value #21100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
PaKu5 opened this issue Apr 17, 2025 · 2 comments
Open

[BUG] [java] wrong enum default value #21100

PaKu5 opened this issue Apr 17, 2025 · 2 comments

Comments

@PaKu5
Copy link

PaKu5 commented Apr 17, 2025

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
@jpfinne
Copy link
Contributor

jpfinne commented Apr 19, 2025

@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" ?

@PaKu5
Copy link
Author

PaKu5 commented Apr 23, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants