Set Min and Max values between 0.1 and 0.9 in OpenApi

Viewed 42

I defined missingRatio variable in my openApi yaml file as below. Openapi version is "3.0.0"

api.yaml

missingRatio:
  type: number
  format: float
  minimum: 0.1
  maximum: 0.3
  default: 0.2
  multipleOf: 0.1
  description: "Ratio of data to remove for validation"

But unfortunately in generated code min and max values are set to 0. So when I use, it only accepts 0 as value.

generated code

  /**
   * Ratio of data to remove for validation
   * minimum: 0
   * maximum: 0
   * @return missingRatio
  **/
  @ApiModelProperty(value = "Ratio of data to remove for validation")

  @DecimalMin("0") @DecimalMax("0") 
  public Float getMissingRatio() {
    return missingRatio;
  }

How Can I resolve this issue?

1 Answers

Maybe you should remove the format: float from your schema. Floating point arithmetic is unreliable. Leave the field as BigDecimal and see if it works.

@DecimalMin and @DecimalMax work for BigDecimals and not float as per documentation

https://docs.oracle.com/javaee/7/api/javax/validation/constraints/DecimalMin.html

Excerpt from Oracle docs:

Note that double and float are not supported due to rounding errors (some providers might provide some approximative support).

null elements are considered valid.

Related