Search code examples
javajsonbean-validationjsonschema2pojo

jsonchema2pojo: How the minLength, maxLength bounds applied on generated Java Pojo class?


I want to know, how the bounds provided in JSON schema gets transformed in to java POJO class?

For example, below is definitions.json file contents -

{
"definitions": {

    "address": {
        "type": "object",
        "properties": {
            "street": { "type": "string", "minLength" : 30, "maxLength" : 100 },
            "city": { "type": "string", "minLength" : 30, "maxLength" : 100 },
            "state": { "type": "string", "minLength" : 30, "maxLength" : 100  }
        }
}

}
}

The above json schema transformed in to pojo class using jsonschema2pojo utility as below -

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
    "street",
    "city",
    "state"
})
public class Address {

    @JsonProperty("street")
    private String street;
    @JsonProperty("city")
    private String city;
    @JsonProperty("state")
    private String state;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * 
     * @return
     *     The street
     */
    @JsonProperty("street")
    public String getStreet() {
        return street;
    }

    /**
     * 
     * @param street
     *     The street
     */
    @JsonProperty("street")
    public void setStreet(String street) {
        this.street = street;
    }

    /**
     * 
     * @return
     *     The city
     */
    @JsonProperty("city")
    public String getCity() {
        return city;
    }

    /**
     * 
     * @param city
     *     The city
     */
    @JsonProperty("city")
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * 
     * @return
     *     The state
     */
    @JsonProperty("state")
    public String getState() {
        return state;
    }

    /**
     * 
     * @param state
     *     The state
     */
    @JsonProperty("state")
    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    @Override
    public int hashCode() {
        ..... Generated implementation
    }

    @Override
    public boolean equals(Object other) {
        .....Generated Implementation
    }

}

Now, we don't see the minLength and maxLength bounds getting implemented anywhere in the pojo class.

My questions is, does the the bounds ignored while transforming in to a java pojo? Is there any way to make those bounds accounted in to pojo class?

Also, JSON schema definition can have pattern to validate the value of a node, for example -

    {
    "type": "object",
    "properties": {
        "name": {
        "type": "string",
        "pattern": "^[xX]?[0-9a-fA-F]{32}$"
        },
        "address": { "$ref": "definations.json#/definitions/address" }

    }
}

Does the pattern gets ignored while transforming to java pojo class? Is there any way to make these patterns accounted in to Java POJO class?


Solution

  • The value of minLength/maxLength should be in double quotation.

    See the example provided in the documentation for the default value: { "type":"integer", "default":"100"}

    For the pattern case, check the availability of the Maven plugin, CLI and Ant task that allow JSR-303 annotations to be activated via a config argument.

    When activated, the following JSR-303 annotations will be generated:

    Schema rule Annotation maximum @DecimalMax minimum @DecimalMin minItems,maxItems @Size minLength,maxLength @Size pattern @Pattern ...