In Swagger CodeGen, how do you specify a field maximum with decimal places?

1.4k views Asked by At

We're using Java 11 with the following version of Swagger Codegen

        <plugin>
            <groupId>io.swagger.codegen.v3</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>3.0.35</version>

I have this in my OpenAPI 3 spec for a particular DTO field,

    amount:
      type: number
      format: double
      maximum: 99999999.99
      multipleOf: 0.01

Upon running the plugin, the DTO is generated without the decimal places

  /**
   * Get amount
   * maximum: 99999999
   * @return amount
  **/
  @Schema(required = true, description = "")
  @NotNull

   @DecimalMax("99999999")   public Double getAmount() {
    return amount;
  }

As a result, when I submit a value for my listed maximum, 99999999.99, I get this error

"errorMessage": "must be less than or equal to 99999999"

What's the proper way in the openAPI spec and Swagger code gen to have a maximum field with decimal places included afterwards?

2

There are 2 answers

1
D.S. On

Can you try this?

amount:
  type: number
  format: double
  exclusiveMaximum: 100000000
  multipleOf: 0.01
0
djmonki On

Try this based on format double (am aware that DTO is not generating decimal), add it within the swagger-codegen-maven-plugin configuration in your pom.xml:

<configuration>
   <typeMappings>
     <typeMapping>Double=java.math.BigDecimal</typeMapping>
   </typeMappings>
</configuration>