Skip to content

[BUG] openapi-generator generates invalid code when float fields are type mapped to BigDecimal and have default value #12883

@makomika

Description

@makomika

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

We are using openapi-generator maven plugin's spring generator to generate code from openapi specification (yaml). Because pf the rounding issues we map the float fields to BigDecimal data type using TypeMapping as suggested in #5587
In most of the cases it works fine, but for one of the affected float fields of the spec also a default value is defined (default: 1). In the generated code the field has BigDecimal type as expected, but the float value "1f" is assigned which leads to a compilation error.

Expected would be to use BigDecimal.ONE or such as default value.

openapi-generator version

6.0.1

OpenAPI declaration file content or url

We are using maven plugin which has the configuration below

pluginManagement:

                <plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>6.0.1</version>
                    <configuration>
                        <output>target/generated-sources</output>
                        <generatorName>spring</generatorName>
                        <library>spring-boot</library>
                        <generateApis>false</generateApis>
                        <apisToGenerate/>
                        <generateApiDocumentation>false</generateApiDocumentation>
                        <generateApiTests>false</generateApiTests>
                        <generateModels>true</generateModels>
                        <modelPackage>bic.api.model</modelPackage>
                        <modelsToGenerate/>
                        <generateModelDocumentation>false</generateModelDocumentation>
                        <generateModelTests>false</generateModelTests>
                        <generateAliasAsModel>true</generateAliasAsModel>
                        <generateSupportingFiles>false</generateSupportingFiles>
			            <typeMappings>
			                <typeMapping>float=java.math.BigDecimal</typeMapping>
			                <typeMapping>double=java.math.BigDecimal</typeMapping>
			            </typeMappings>
			            <instantiationTypes>
			                 <instantiationType>float=java.math.BigDecimal</instantiationType>
			            </instantiationTypes>
                        <configOptions>
                            <serializationLibrary>jackson</serializationLibrary>
                            <dateLibrary>java8</dateLibrary>
                            <performBeanValidation>true</performBeanValidation>
                            <sourceFolder>/</sourceFolder>
                            <useBeanValidation>true</useBeanValidation>
                        </configOptions>
                    </configuration>
                </plugin>

                    <execution>
                        <id>tmf678billcalculation</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/api/${tmf678billitems-api.filename}</inputSpec>
                        </configuration>
                    </execution>

plugins:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>tmf678billcalculation</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/api/${tmf678billitems-api.filename}</inputSpec>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Generation Details

S. above

Steps to reproduce

Used openapi-spec field definition:

    Quantity:
      type: object
      description: An amount in a given unit
      properties:
        amount:
          default: 1
          type: number
          format: float
          description: Numeric value in a given unit
        units:
          type: string
          description: Unit

Generate sources from a such specification and the result is

  @JsonProperty("amount")
  private java.math.BigDecimal amount = 1f;
Related issues/PRs

For Kotlin the same issue already fixed:
#10866

Suggest a fix

Use a "new BigInteger()" as default

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