$ref resolution without Spring framework
See original GitHub issueHi,
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:
- Created 4 years ago
- Comments:12 (4 by maintainers)
Top GitHub Comments
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.
Hi!
I couldn’t find the
pom.xml
in your project so just guessing here, but probably found the problem: Try this:Explanation: the
load(rawSchema)
call that you used is actually a static method of theSchemaLoader
class, not an instance method, so it doesn’t use the configuration you set up on the instance. Theload()
method returning aSchema.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.