2

I am trying to using the jsonschema2pojo to generate Java classes. I am running into trouble in using the '$ref' tag to refer other schemas in my parent schema.


Device.json

{
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "id": "device:v1",
    "name": "device",
    "properties": {
        "components": {
            "$ref": "spec.json"
            },
        "usage": {
            "$ref": "spec.json"
            }
    }
},
"required": true,
"title": "Device",
"type": "object"
}

Here is my spec.json

{
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "id": "spec:v1",
    "name": "spec",
    "properties": {
        "content": {
            "description" : "Content",
            "type": "string",
            "required": false
    }
},
"required": true,
"title": "spec",
"type": "object"
}

Now I expected the following java class to be created:

public class Device {
    private Spec components,
    private Spec usage

    .....
}

and

public class Spec {
    private String content
}

but I get

public class Device {
    private Components components,
    private Components usage

    .....
}

and

public class Components {
    private String content
}

What am I doing wrong?

zambro
  • 414
  • 1
  • 6
  • 17

1 Answers1

1

Thanks to the answer from here, by joelittlejohn. I got this to work using the javaType in spec.json


Device.json

{
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "id": "device:v1",
    "name": "device",
    "properties": {
        "components": {
            "$ref": "spec.json",
            "type": "object",
            },
        "usage": {
            "$ref": "spec.json",
            "type": "object",
            }
    }
},
"required": true,
"title": "Device",
"type": "object"
}

Spec.json

{
    "$schema": "http://json-schema.org/draft-03/hyper-schema",
    "additionalProperties": false,
    "javaType": "my-package-name.Spec"
    "id": "spec:v1",
    "name": "spec",
    "properties": {
        "content": {
            "description" : "Content",
            "type": "string",
            "required": false
    }
},
"required": true,
"title": "spec",
"type": "object"
}
zambro
  • 414
  • 1
  • 6
  • 17