joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Unable to generate javax.validation.constraints.DecimalMin and DecimalMax for type number #1583

Closed abramsz closed 10 months ago

abramsz commented 10 months ago

I try to validate the conversion from json schema to java code in https://www.jsonschema2pojo.org/. Only type of integer in schema can be used to geneate @DecimalMin("10"), for type of number, nothing has been generated.

With the following settings and json schema. image

The following java code has been generated. image

Thanks in advance.

joelittlejohn commented 10 months ago

Interestingly, the javadoc says this:

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

So I guess this is why this applies only to integers. I guess we can add the annotation anyway. If it is not supported by some validators, I assume they will just ignore it.

abramsz commented 10 months ago

I have found it here. From MinimumMaximumRule.java, it is prefer to use type BigDecimal instead of Double and Float.Let's close it.

    private boolean isApplicableType(JFieldVar field) {
        try {
            Class<?> fieldClass = Class.forName(field.type().boxify().fullName());
            // Support Strings and most number types except Double and Float, per docs on DecimalMax/Min annotations
            return String.class.isAssignableFrom(fieldClass) ||
                    (Number.class.isAssignableFrom(fieldClass) &&
                            !Float.class.isAssignableFrom(fieldClass) && !Double.class.isAssignableFrom(fieldClass));
        } catch (ClassNotFoundException ignore) {
            return false;
        }
    }