Search code examples
javajsonschemajsonschema2pojo

Arrays are not being generated List<ComplexType> but List<Object>


I have a complex schema for which I am generating POJO's. However, I noticed that complex types are not being generated for items inside type array although they are marked type object. I did a quick test with very simple schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "javaType": "com.walmart.services.tesseract.service.request.models.inkiru.PaymentHead",
  "properties": {
    "payments": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "paymentHandle": {
              "type": "string"
            },
            "txndate ": {
              "type": "string"
            },
            "ordernumber": {
              "type": "string"
            },
            "stgOrderNumber": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}

This generates the following POJO

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "payments"
})
public class PaymentHead {

    @JsonProperty("payments")
    private List<Object> payments = new ArrayList<Object>();

    @JsonProperty("payments")
    public List<Object> getPayments() {
        return payments;
    }

    @JsonProperty("payments")
    public void setPayments(List<Object> payments) {
        this.payments = payments;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(PaymentHead.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
        sb.append("payments");
        sb.append('=');
        sb.append(((this.payments == null)?"<null>":this.payments));
        sb.append(',');
        if (sb.charAt((sb.length()- 1)) == ',') {
            sb.setCharAt((sb.length()- 1), ']');
        } else {
            sb.append(']');
        }
        return sb.toString();
    }

}

However, instead of Object, it should be complexType. Isn't it? Am I missing something?


Solution

  • The correct schema should be. Notice that items is an object in schema, not array.

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "javaType": "com.walmart.services.tesseract.service.request.models.inkiru.PaymentHead",
      "properties": {
        "payments": {
          "type": "array",
          "items":
            {
              "type": "object",
              "properties": {
                "paymentHandle": {
                  "type": "string"
                },
                "txndate ": {
                  "type": "string"
                },
                "ordernumber": {
                  "type": "string"
                },
                "stgOrderNumber": {
                  "type": "string"
                }
              }
            }
        }
      }
    }