Search code examples
javajsonschemajsonschema2pojo

jsonschema2pojo: type creation for list of objects


I am using jsonschema2pojo (via gradle) to generate pojo from an external json schema that has a property (inside another property of type "object") with the following definition (field names and values changed but all types are identical):

"alternativeText": {
                    "type": "array",
                    "items": [{
                        "type": "object",
                        "required": [
                            "code",
                            "language",
                            "value"
                        ],
                        "properties": {
                            "code": {                               
                                "type": "integer",
                                "enum": [1, 2, 3, 4]                                
                            },
                            "language": {
                                "type": "string",
                                "enum": ["de-DE", "en-US"]
                            },
                            "value": {
                                "type": "string"
                            }
                        }
                    }],

which generates the following java code

 @JsonProperty("alternativeText")
    @Valid
    @NotNull
    private List<Object> alternativeText = new ArrayList<Object>();
    @JsonIgnore
    @Valid
    private Map<String, Object> additionalProperties = new LinkedHashMap<String, Object>();

This interface is not ideal, especially when the consumer is going to validate against the enum values for "code" and "language".

We have similar issues when an enum is in an array, then the API generates List<Object> instead of e.g. List<SomeEnumType>.

Is there anything that can be configured to improve the semantics of the generated code? For other properties with "enum" (and object for that matter!) it works correctly, it seems like the array of enum or array of objects gets overly simplified.


Solution

  • It looks like you have accidentally used the (old) tuple syntax for items. Try replacing this:

    "items": [{
    

    with this

    "items": {
    

    Your items schema should not be inside an array.