Skip to content
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

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

Open
5 tasks done
makomika opened this issue Jul 15, 2022 · 0 comments

Comments

@makomika
Copy link

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

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

1 participant