joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc

Home Page:http://www.jsonschema2pojo.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

abramsz opened this issue · comments

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.

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.

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;
        }
    }