Vinaya Nayak
Vinaya Nayak

Reputation: 1301

Json schema validation fails with MalformedURLException: unknown protocol: classpath error

I have the following dependencies in my pom.xml

 <!-- https://github.com/everit-org/json-schema -->
               <dependency>
                   <groupId>com.github.everit-org.json-schema</groupId>
                   <artifactId>org.everit.json.schema</artifactId>
                   <version>1.11.1</version>
               </dependency>

               <!-- https://mvnrepository.com/artifact/org.json/json -->
               <dependency>
                   <groupId>org.json</groupId>
                   <artifactId>json</artifactId>
                   <version>20190722</version>
               </dependency>

this is my json schema

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "id": "test",
    "title": "test-json validation",
    "description": "This schema should define the structure of the test json",
    "allOf": [
        {
            "$ref": "classpath:/jsonSchema/header/test1.json#/definitions/test1"
        },
        {
            "$ref": "classpath:/jsonSchema/rows/test2.json#/definitions/test2"
        }
    ],
    "properties": {
        "version": {
            "type": "array",
            "items": {
                "type": "string",
                "enum": [
                    "2.0",
                    "2.1"
                ]
            }
        }
    },
    "required": [
        "version"
    ]
}

and this is what I am trying to achieve

public Schema createSchema(String schemaPath) throws IOException {

        Schema schema = null;
        try (InputStream inputStream = new ClassPathResource(schemaPath).getInputStream()) {
            JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
            schema = SchemaLoader.load(rawSchema);
        }
        return schema;
    }

And I get the following exception:

Caused by: java.io.UncheckedIOException: java.net.MalformedURLException: unknown protocol: classpath at org.everit.json.schema.loader.internal.DefaultSchemaClient.get(DefaultSchemaClient.java:20) at org.everit.json.schema.loader.JsonPointerEvaluator.executeWith(JsonPointerEvaluator.java:78) at org.everit.json.schema.loader.JsonPointerEvaluator.lambda$forURL$1(JsonPointerEvaluator.java:121) at org.everit.json.schema.loader.JsonPointerEvaluator.query(JsonPointerEvaluator.java:151) at org.everit.json.schema.loader.ReferenceLookup.lookup(ReferenceLookup.java:173) at org.everit.json.schema.loader.ReferenceSchemaExtractor.extract(SchemaExtractor.java:193) at org.everit.json.schema.loader.AbstractSchemaExtractor.extract(SchemaExtractor.java:113) at org.everit.json.schema.loader.SchemaLoader.runSchemaExtractors(SchemaLoader.java:383) at org.everit.json.schema.loader.SchemaLoader.loadSchemaObject(SchemaLoader.java:360)

Do I need to set the resolution scope?

Upvotes: 0

Views: 1245

Answers (1)

Vinaya Nayak
Vinaya Nayak

Reputation: 1301

I managed to solve the problem by setting the resolution scope

    public Schema createSchema(String schemaPath) throws IOException {

        Schema schema = null;

        try (InputStream inputStream = new ClassPathResource(schemaPath).getInputStream()) {
            JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
            SchemaLoader schemaLoader = SchemaLoader.builder()
                    .schemaClient(SchemaClient.classPathAwareClient())
                    .schemaJson(rawSchema)
                    .resolutionScope("classpath://jsonSchema") // setting the default resolution scope
                    .build();
            schema = schemaLoader.load().build();
        }
        return schema;
    }

Upvotes: 1

Related Questions