5

I need to generate Java classes from a JSON schema file and came across jsonschema2pojo. However, I encountered a "problem" when using the ref keyword.

For example, if I use the following schema from http://spacetelescope.github.io/understanding-json-schema/structuring.html#extending:

{
  "$schema": "http://json-schema.org/draft-04/schema#",

  "definitions": {
    "address": {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" },
        "state":          { "type": "string" }
      },
      "required": ["street_address", "city", "state"]
    }
  },

  "type": "object",

  "properties": {
    "billing_address": { "$ref": "#/definitions/address" },
    "shipping_address": { "$ref": "#/definitions/address" }
  }
}

As expected, it generated a class named whatever you want to call it, containing an attribute billingAddress and an attribute shippingAddress.

However, it also generated two separate classes BillingAddress and ShippingAddress even though both attributes are referencing to address. Hence, I would rather have both attributes of type Address.

Is this possible to achieve with jsonschema2pojo?

user2035039
  • 961
  • 3
  • 16
  • 30

1 Answers1

8

Update

After getting a better understanding of javaType from here. I get the expected result by just adding a javaType in your Address definition.

{
  "$schema": "http://json-schema.org/draft-04/schema#",

  "definitions": {
      "address": {
      "type": "object",
      "javaType": "Address",
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" },
        "state":          { "type": "string" }
      },
      "required": ["street_address", "city", "state"]
    }
  },
  "type": "object",
  "properties": {
    "billing_address": { "$ref": "#/definitions/address" },
    "shipping_address": { "$ref": "#/definitions/address" }
  }
}

Answer with two files

You need to use javaType in your Address.json and use $ref for your billing_address and shipping address. I would suggest you to separate the address definition into a separate json and then use that in your billing_address and shipping_address.


Address.json

{
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "javaType": "whatever-package-name-you-have.Address"
    "type": "object",
    "properties": {
    "street_address": { "type": "string", "required":true},
    "city":           { "type": "string", "required":true },
    "state":          { "type": "string", "required":true }
  }
}

MainClass.json

{
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "type": "object",
    "properties": {
     "billing_address": {
           "$ref":"Address.json",
           "type": "object",
           "required": false
          },
     "shipping_address": {
           "$ref":"Address.json",
           "type": "object",
           "required": false
          }
     }
}
zambro
  • 414
  • 1
  • 6
  • 17