简体   繁体   English

openapi 3.0 有效的最小值和最大值

[英]openapi 3.0 valid minimum and maximum values

I have below schema definition to represent commission amount in my openapi contract.我有以下模式定义来表示我的 openapi 合同中的commission amount

commissionAmount:
  type: number
  minimum: -99999.99
  maximum: 99999.99

Generated Code:生成的代码:

@Valid
@DecimalMin("-99999.99") @DecimalMax("99999.99") 
public BigDecimal getCommissionAmount() {
  return commAmt;
}

The generated code is good and as expected.生成的代码很好,符合预期。 I just wanted to know are these -99999.99 and 99999.99 valid values for minimum and maximum .我只是想知道这些-99999.9999999.99minimummaximum有效值。

The reason for asking this question is it does not check the limit in the fractional part.问这个问题的原因是它没有检查小数部分的限制。 For example, I expect 12345.678 is invalid , 12345.67 is valid.例如,我希望12345.678无效, 12345.67有效。 But it marks both as valid.但它将两者都标记为有效。

I read @Digits is used to check for the digit limit of integer and fractional part.我读过@Digits用于检查整数和小数部分的位数限制。 How do I tell openapi-generator-maven-plugin to annotate Digits as well?我如何告诉openapi-generator-maven-plugin也注释Digits

Expected Generated Code:预期生成的代码:

@Valid
@Digits(integer = 5, fraction = 2)
@DecimalMin("-99999.99") @DecimalMax("99999.99") 
public BigDecimal getCommissionAmount() {
  return commAmt;
}

The way to specify this in OpenAPI would be using multipleOf :在 OpenAPI 中指定它的方法是使用multipleOf

commissionAmount:
  type: number
  minimum: -99999.99
  maximum: 99999.99
  multipleOf: 0.01

However, using the OpenAPI Generator will not produce an annotation for this.但是,使用 OpenAPI Generator不会为此生成注释 The reason being is that there is no javax.validation annotation that can represent multipleOf effectively (imagine trying to express multipleOf: 0.02 - @Digits would be insufficient).原因是没有javax.validation注释可以有效地表示multipleOf (想象一下试图表达multipleOf: 0.02 - @Digits是不够的)。

However, you can create your own annotation as this user has: https://github.com/OpenAPITools/openapi-generator/issues/2192#issuecomment-575132233但是,您可以像该用户一样创建自己的注释: https : //github.com/OpenAPITools/openapi-generator/issues/2192#issuecomment-575132233

With the following annotation and validator:使用以下注释和验证器:

@Target({METHOD, FIELD})
@Retention(RUNTIME)
@Repeatable(MultipleOf.List.class)
@Constraint(validatedBy = MultipleOfValidator.class)
public @interface MultipleOf {

    double value();

    String message() default "{error.multipleOf}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default { };

    @Target({ METHOD, FIELD })
    @Retention(RUNTIME)
    @Documented
    @interface List {
        MultipleOf[] value();
    }

}
public class MultipleOfValidator implements ConstraintValidator<MultipleOf, Number> {

    private double value;

    @Override
    public void initialize(MultipleOf constraintAnnotation) {
        this.value = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Number value, ConstraintValidatorContext context) {
        return (value == null) || (value.doubleValue() / this.value) % 1 == 0;
    }

}

You would then be able to fork the generator and add your new annotation to the template: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/beanValidationCore.mustache然后,您就可以分叉生成器并将新注释添加到模板中: https : //github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/ beanValidationCore.mustache

With a line like this:像这样的一行:

{{#multipleOf}}@MultipleOf({{multipleOf}}){{/multipleOf}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM