1

Hi I'm using the JSON Schema evaluator in version 2.2.6 to validate my server responses. These responses can contain single objects of type A, B or C, but also composite objects, e.g., D containing an array of A objects. To reuse the schema definitions of each object, I started to describe all entities in the same file as described here. Now my problem is, that I have to reference one of those single objects when validating the response.

Here is my (not) SWE.

JSON schema file:

{
    "id":"#root",
    "properties": {
        "objecta": {
            "type": "object",
            "id":"#objecta",
            "properties": {
                "attribute1": {"type": "integer"},
                "attribute2": {"type": "null"},
            },
            "required": ["attribute1", "attribute2"]
        },
        "objectb": {
            "type": "object",
            "id":"#objectb",
            "properties": {
                "attribute1": {"type": "integer"},
                "attribute2": {
                    "type": "array",
                        "items": {
                            "$ref": "#/objecta"
                        }
                    }
                }
            },
            "required": ["attribute1", "attribute2"]
        },
    }
}

Now I want to validate a server response containing object B. For this, I tried the following:

public class SchemeValidator {

    public static void main(String[] args) {

        String jsonData = pseudoCodeFileLoad("exampleresponse/objectb.txt");
        final File jsonSchemaFile = new File("resources/jsonschemes/completescheme.json");
        final URI uri = jsonSchemaFile.toURI();

        ProcessingReport report = null;

        try {
            JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
            final JsonSchema schema = factory.getJsonSchema(uri.toString() + "#objectb");
            JsonNode data = JsonLoader.fromString(jsonData);
            report = schema.validate(data);
        } catch (JsonParseException jpex) {
            // ... handle parsing errors etc.
        }
    }
}

The problem is that the scheme is not loaded correctly. I either get no error (even for invalid responses) or I get fatal: illegalJsonRef as the scheme seems to be empty. How can I use the schema of object b in my Java code? Thank you!!

Community
  • 1
  • 1
strauberry
  • 4,189
  • 5
  • 34
  • 50

1 Answers1

0

It looks like your $ref is incorrect. It needs to be a relative reference from the base of the JSON Schema file (see here).

So your JSON schema would become:

{
"id":"#root",
"properties": {
    "objecta": {
        "type": "object",
        "id":"#objecta",
        "properties": {
            "attribute1": {"type": "integer"},
            "attribute2": {"type": "null"},
        },
        "required": ["attribute1", "attribute2"]
    },
    "objectb": {
        "type": "object",
        "id":"#objectb",
        "properties": {
            "attribute1": {"type": "integer"},
            "attribute2": {
                "type": "array",
                    "items": {
                        "$ref": "#/properties/objecta"
                    }
                }
            }
        },
        "required": ["attribute1", "attribute2"]
    },
}

}

I've added '/properties' to your $ref. It's operating like XPath to the definition of the object in the schema file.

Cei
  • 1
  • 1