question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$ref resolution without Spring framework

See original GitHub issue

Hi,

I’m tring to use the json-schema library in a web application that doesn’t use Spring framework. And I encountered some problems about the “$ref”.


Foreword:

The version of json-schema that I’m using is the last release (1.11.1) downloaded from the releases page.

I’ve tried both “classpath://” and “classpath:///”, and there is no difference between them.


1st. “no protocol”

I did something similar with what the doc in the README file.

  • the Java code:
String jschRoot = "/directory_in_classpath/to/store/json_files";
SchemaLoader schemaLoader = SchemaLoader.builder()
                .schemaClient(SchemaClient.classPathAwareClient())
                .resolutionScope("classpath://" + jschRoot)
                .build();

InputStream inStream = Test.class
                .getClassLoader()
                .getResourceAsStream(jschRoot + "/schema.json");

JSONObject rawSchema = new JSONObject(new JSONTokener(inStream));
Schema schema = schemaLoader.load(rawSchema);
schema.validate(aJsonObject);
  • the JSON schema
{
        "type" : "object",
        "properties" : {
                "something" : {"$ref" : "predefined.json" }
        }
}
  • and the exception
java.io.UncheckedIOException: java.net.MalformedURLException: no protocol: predefined.json
	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)
	at org.everit.json.schema.loader.JsonValue$Multiplexer.requireAny(JsonValue.java:46)
	at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:434)
	at org.everit.json.schema.loader.SchemaLoader.loadChild(SchemaLoader.java:438)
	at org.everit.json.schema.loader.ObjectSchemaLoader.addPropertySchemaDefinition(ObjectSchemaLoader.java:70)
	at org.everit.json.schema.loader.ObjectSchemaLoader.lambda$populatePropertySchemas$9(ObjectSchemaLoader.java:64)
	at org.everit.json.schema.loader.JsonObject.iterateOnEntry(JsonObject.java:84)
	at org.everit.json.schema.loader.JsonObject.lambda$forEach$1(JsonObject.java:79)
	at java.util.HashMap$EntrySet.forEach(HashMap.java:1044)
	at org.everit.json.schema.loader.JsonObject.forEach(JsonObject.java:79)
	at org.everit.json.schema.loader.ObjectSchemaLoader.populatePropertySchemas(ObjectSchemaLoader.java:61)
	at org.everit.json.schema.loader.ObjectSchemaLoader.lambda$load$0(ObjectSchemaLoader.java:32)
	at java.util.Optional.ifPresent(Optional.java:159)
	at org.everit.json.schema.loader.ObjectSchemaLoader.load(ObjectSchemaLoader.java:32)
	at org.everit.json.schema.loader.AbstractSchemaExtractor.buildObjectSchema(SchemaExtractor.java:138)
	at org.everit.json.schema.loader.TypeBasedSchemaExtractor.loadForExplicitType(SchemaExtractor.java:277)
	at org.everit.json.schema.loader.JsonValue$Multiplexer.requireAny(JsonValue.java:46)
	at org.everit.json.schema.loader.TypeBasedSchemaExtractor.extract(SchemaExtractor.java:247)
	at org.everit.json.schema.loader.AbstractSchemaExtractor.extract(SchemaExtractor.java:113)
	at org.everit.json.schema.loader.SchemaLoader.runSchemaExtractors(SchemaLoader.java:396)
	at org.everit.json.schema.loader.SchemaLoader.loadSchemaObject(SchemaLoader.java:360)
	at org.everit.json.schema.loader.JsonValue$Multiplexer.requireAny(JsonValue.java:46)
	at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:434)
	at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:281)
	at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:264)
	at com.test.Test.main(Test.java:33)
Caused by: java.net.MalformedURLException: no protocol: predefined.json
	at java.net.URL.<init>(URL.java:593)
	at java.net.URL.<init>(URL.java:490)
	at java.net.URL.<init>(URL.java:439)
	at org.everit.json.schema.loader.internal.DefaultSchemaClient.get(DefaultSchemaClient.java:18)
	... 33 more

According to the README file, the referred JSON file “predefined.json” should be found at “/directory_in_classpath/to/store/json_files/predefined.json” in the classpath. But, it didn’t work as expected. Usually, if a file could not be found, something like “No such file or directory” should be thrown, seems it does not even pass the verification. And the same thing happened when I change the protocol form “classpath://” to “file://” as long as I’m using a relative path.


2nd. “unknown protocol: classpath”

After encounterred the previous problem, I changed the value of “$ref” field in the JSON schema file from a relative path to a absolute path: “classpath://directory_in_classpath/to/store/json_files/predefined.json”.

Then, that’s another exception:

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)
	at org.everit.json.schema.loader.JsonValue$Multiplexer.requireAny(JsonValue.java:46)
	at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:434)
	at org.everit.json.schema.loader.SchemaLoader.loadChild(SchemaLoader.java:438)
	at org.everit.json.schema.loader.ObjectSchemaLoader.addPropertySchemaDefinition(ObjectSchemaLoader.java:70)
	at org.everit.json.schema.loader.ObjectSchemaLoader.lambda$populatePropertySchemas$9(ObjectSchemaLoader.java:64)
	at org.everit.json.schema.loader.JsonObject.iterateOnEntry(JsonObject.java:84)
	at org.everit.json.schema.loader.JsonObject.lambda$forEach$1(JsonObject.java:79)
	at java.util.HashMap$EntrySet.forEach(HashMap.java:1044)
	at org.everit.json.schema.loader.JsonObject.forEach(JsonObject.java:79)
	at org.everit.json.schema.loader.ObjectSchemaLoader.populatePropertySchemas(ObjectSchemaLoader.java:61)
	at org.everit.json.schema.loader.ObjectSchemaLoader.lambda$load$0(ObjectSchemaLoader.java:32)
	at java.util.Optional.ifPresent(Optional.java:159)
	at org.everit.json.schema.loader.ObjectSchemaLoader.load(ObjectSchemaLoader.java:32)
	at org.everit.json.schema.loader.AbstractSchemaExtractor.buildObjectSchema(SchemaExtractor.java:138)
	at org.everit.json.schema.loader.TypeBasedSchemaExtractor.loadForExplicitType(SchemaExtractor.java:277)
	at org.everit.json.schema.loader.JsonValue$Multiplexer.requireAny(JsonValue.java:46)
	at org.everit.json.schema.loader.TypeBasedSchemaExtractor.extract(SchemaExtractor.java:247)
	at org.everit.json.schema.loader.AbstractSchemaExtractor.extract(SchemaExtractor.java:113)
	at org.everit.json.schema.loader.SchemaLoader.runSchemaExtractors(SchemaLoader.java:396)
	at org.everit.json.schema.loader.SchemaLoader.loadSchemaObject(SchemaLoader.java:360)
	at org.everit.json.schema.loader.JsonValue$Multiplexer.requireAny(JsonValue.java:46)
	at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:434)
	at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:281)
	at org.everit.json.schema.loader.SchemaLoader.load(SchemaLoader.java:264)
	at com.test.Test.main(Test.java:33)
Caused by: java.net.MalformedURLException: unknown protocol: classpath
	at java.net.URL.<init>(URL.java:600)
	at java.net.URL.<init>(URL.java:490)
	at java.net.URL.<init>(URL.java:439)
	at org.everit.json.schema.loader.internal.DefaultSchemaClient.get(DefaultSchemaClient.java:18)
	... 33 more


BTW, is there any additional documentation except javadoc? I tried to find more documentations but the only thing I’ve found is the javadoc.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Mr-indescribablecommented, Apr 9, 2019

Uh, I find that this project is an implementation of json-schema.org’s JSON schema specification.

The doc I was searching for is in json-schema.org actually.

1reaction
erosbcommented, Apr 5, 2019

Hi!

I couldn’t find the pom.xml in your project so just guessing here, but probably found the problem: Try this:

InputStream inStream = Test.class
	.getClassLoader()
	.getResourceAsStream(jschRoot + "/schema.json");

JSONObject rawSchema = new JSONObject( new JSONTokener(inStream) );
SchemaLoader schemaLoader = SchemaLoader.builder()
	.schemaClient(SchemaClient.classPathAwareClient())
	.resolutionScope("classpath://" + jschRoot)
	.schemaJson(rawSchema)
	.build();

Schema schema = schemaLoader.load().build();

Explanation: the load(rawSchema) call that you used is actually a static method of the SchemaLoader class, not an instance method, so it doesn’t use the configuration you set up on the instance. The load() method returning a Schema.Builder is the instance method you have to call so the loading will happen with the configuration you set up (I will try to make it more clear in the readme).

The static load(rawSchema) method is left around mostly for compatibility reasons, this was the way how the very first version of the library was implemented, so we don’t remove it, it just loads the schema with a pure configuration which is compatible with the 1.0.0 version (it isn’t a very useful configuration though).

Documentation: I try to put as much into the README.md as I find it useful, but of course I’m not a user of the library, so I can’t really tell what is missing. Feel free to ask. Javadoc-wise we are obviously lagging behind. Contributions are welcome. The thing is that this project is not sponsored actively by anyone so I just support it in my free time, which is more and more limited.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Framework Reference Documentation
This document is a reference guide to Spring Framework features. ... Make a Java method execute in a database transaction without having to...
Read more >
Spring Cloud Config Client Without Spring Boot - Stack Overflow
Currently we load properties files as http resources to give us a single source of property placeholder config resolution. Im investigating ...
Read more >
Circular Dependencies in Spring - Baeldung
We rely on other people's code in our own work. Every day. It might be the language you're writing in, the framework you're...
Read more >
Key Components and Internals of Spring Boot Framework
If we develop Spring applications in Spring Boot,then We dont need to define single XML configuration and almost no or minimal Annotation ...
Read more >
Java Spring Framework vulnerabilities (CVE-2022 ... - PTC
Applicability : See Resolution Additional Reference : Tanzu vmware also ... as a Spring Boot executable jar, i.e. the default, it is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found