OpenAPITools / OpenAPITools/openapi-generator
[BUG] openapi-generator generates invalid code when float fields are type mapped to BigDecimal and have default value
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Maven plugin's spring generator and reproduce the issue using the provided Quantity schema and float-to-java.math.BigDecimal type mapping. Compare the generated model default with the Kotlin fix referenced in #10866. Done means the generated Java model uses a valid BigDecimal default and a regression test covers the mapped float default case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, spring
- Domain
- api, backend, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100