Skip to content

[BUG][JAVA] Inner Enum generation with BigDecimal values results in compilation error #20188

@timon-sbr

Description

@timon-sbr

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

Hello,

I recently updated openapi-generator-maven-plugin from Version 7.9.0 to 7.10.0. The new version seems to have problems when generating inner enums with BigDecimal fields. The generated Code produces compiler errors, because it tries to create instances of BigDecimal with

BigDecimal.valueOf(new BigDecimal("0"))

BigDecimals valueOf method has no such singnature.

openapi-generator version

Version 7.10.0

Generation Details

The maven configuration of the generator plugin looks like this:

<configuration>
  <inputSpec>target/generated-sources/openapi/inputspec.yml</inputSpec>
  <output>target/generated-sources</output>
  <generatorName>spring</generatorName>
  <generateApis>true</generateApis>
  <generateSupportingFiles>true</generateSupportingFiles>
  <typeMappings>
    <typeMapping>Double=java.math.BigDecimal</typeMapping>
  </typeMappings>
  <configHelp>false</configHelp>
  <configOptions>
    <basePackage>${genclasses.package}</basePackage>
    <apiPackage>${genclasses.package}.api</apiPackage>
    <modelPackage>${genclasses.package}.model</modelPackage>
    <dateLibrary>java8</dateLibrary>
    <library>spring-boot</library>
    <useSpringBoot3>true</useSpringBoot3>
    <useJakartaEe>true</useJakartaEe>
    <interfaceOnly>true</interfaceOnly>
    <serializableModel>false</serializableModel>
    <skipDefaultInterface>true</skipDefaultInterface>
    <skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
    <snapshotVersion>true</snapshotVersion>
    <useTags>true</useTags>
  </configOptions>
</configuration>
Generated Output

With version 7.9.0 the inner enums are looking good:

public enum TypeEnum {
    NUMBER_0(new BigDecimal("0")),
    NUMBER_1(new BigDecimal("1")),
    NUMBER_2(new BigDecimal("2"));

    private BigDecimal value;

    TypeEnum(BigDecimal value) {
      this.value = value;
    }
  // ... more code
}

Version 7.10.0 produces:

public enum TypeEnum {
    NUMBER_0(BigDecimal.valueOf(new BigDecimal("0"))),
    NUMBER_1(BigDecimal.valueOf(new BigDecimal("1"))),
    NUMBER_2(BigDecimal.valueOf(new BigDecimal("2")));

    private BigDecimal value;

    TypeEnum(BigDecimal value) {
      this.value = value;
    }
  // ... more code
}
Suggest a fix

I found this PR PR-19815 which seems to introduce the issue. The template file modules/openapi-generator/src/main/resources/Java/modelInnerEnum.mustache:26 now defines to use the .valueOf method, which doesn't work for type BigDecimal if the argument also is a BigDecimal.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions