Search code examples
javajsonjsonschemajsonschema2pojo

JSON Schema multiple instances of a property


I'm trying to define a json schema where there are multiple properties that instances of the same object for a Java application endpoint.

A simple example of what I'm trying to do is this

{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",

"properties": {
    "last": {
        "$ref": "#/$def/node"
    },
    "next": {
        "$ref": "#/$def/node"
    }
},
"$def": {
    "node": {
        "type": "object",
        "properties": {
            "item": "object"
        }
    }
}}

The issue is that when I pass this into jsonschema2pojo, it interprets this as last and next being distinct classes rather than instances of a common node object.

-----------------------------------com.example.Last.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Last {

@SerializedName("item")
@Expose
public Object item;

}

-----------------------------------com.example.Next.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Next {

@SerializedName("item")
@Expose
public Object item;

}

-----------------------------------com.example.Node.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Node {

@SerializedName("last")
@Expose
public Last last;
@SerializedName("next")
@Expose
public Next next;

}

Is it possible to have multiple instances of a property in json-schema and would I express this? Or is this a limitation of the jsonschema2pojo tool/plugin? Or does json-schema require multiple instances be placed into an array?


Solution

  • It seems you have to force the same type being used by declaring a specific "javaType" in your definition, which is unfortunately tool-specific (which is something you wanted to avoid according to your comments).

    The following worked on jsonschema2pojo.org:

    Input: JSON Schema

    {
      "type": "object",
      "properties": {
          "last": {
            "$ref": "#/definitions/node"
          },
          "next": {
            "$ref": "#/definitions/node"
          }
      },
      "definitions": {
          "node": {
              "javaType" : "com.example.Node",
              "type": "object",
              "properties": {
                "item": {}
              }
          }
      }
    }
    

    Output: com.example.Example.java

    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example {
    
        @SerializedName("last")
        @Expose
        public Node last;
        @SerializedName("next")
        @Expose
        public Node next;
    
    }
    

    Output: com.example.Node.java

    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Node {
    
        @SerializedName("item")
        @Expose
        public Object item;
    
    }