Skip to content

Commit fa93505

Browse files
authored
Merge pull request #169 from jawaff/fix/#166-uri-support
fix/#166 uri support update
2 parents 2224e26 + 0128c25 commit fa93505

File tree

6 files changed

+19
-13
lines changed

6 files changed

+19
-13
lines changed

src/main/java/com/networknt/schema/BaseJsonValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static JsonSchema obtainSubSchemaNode(final JsonNode schemaNode, final V
8282
else {
8383
final URI uri;
8484
try {
85-
uri = validationContext.getJsonSchemaFactory().getURIFactory().create(node.textValue());
85+
uri = validationContext.getURIFactory().create(node.textValue());
8686
} catch (IllegalArgumentException e) {
8787
return null;
8888
}

src/main/java/com/networknt/schema/JsonSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) {
8181
return currentUri;
8282
} else {
8383
try {
84-
return this.validationContext.getJsonSchemaFactory().getURIFactory().create(currentUri, idNode.asText());
84+
return this.validationContext.getURIFactory().create(currentUri, idNode.asText());
8585
} catch (IllegalArgumentException e) {
8686
throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.ID.getValue(), ValidatorTypeCode.ID, idNode.asText(), currentUri.toString()));
8787
}

src/main/java/com/networknt/schema/JsonSchemaFactory.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private JsonSchema newJsonSchema(final URI schemaUri, final JsonNode schemaNode,
233233

234234
protected ValidationContext createValidationContext(final JsonNode schemaNode) {
235235
final JsonMetaSchema jsonMetaSchema = findMetaSchemaForSchema(schemaNode);
236-
return new ValidationContext(jsonMetaSchema, this);
236+
return new ValidationContext(this.uriFactory, jsonMetaSchema, this);
237237
}
238238

239239
private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
@@ -246,10 +246,6 @@ private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
246246
return jsonMetaSchema;
247247
}
248248

249-
public URIFactory getURIFactory() {
250-
return this.uriFactory;
251-
}
252-
253249
public JsonSchema getSchema(final String schema, final SchemaValidatorsConfig config) {
254250
try {
255251
final JsonNode schemaNode = mapper.readTree(schema);
@@ -286,7 +282,7 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co
286282

287283
final URI mappedUri;
288284
try {
289-
mappedUri = URI.create(map.getOrDefault(schemaUri.toString(), schemaUri.toString()));
285+
mappedUri = this.uriFactory.create(map.getOrDefault(schemaUri.toString(), schemaUri.toString()));
290286
} catch (IllegalArgumentException e) {
291287
logger.error("Failed to create URI.", e);
292288
throw new JsonSchemaException(e);
@@ -297,7 +293,7 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co
297293
final JsonMetaSchema jsonMetaSchema = findMetaSchemaForSchema(schemaNode);
298294

299295
if (idMatchesSourceUri(jsonMetaSchema, schemaNode, schemaUri)) {
300-
return new JsonSchema(new ValidationContext(jsonMetaSchema, this), mappedUri, schemaNode, true /*retrieved via id, resolving will not change anything*/);
296+
return new JsonSchema(new ValidationContext(this.uriFactory, jsonMetaSchema, this), mappedUri, schemaNode, true /*retrieved via id, resolving will not change anything*/);
301297
}
302298

303299
return newJsonSchema(mappedUri, schemaNode, config);

src/main/java/com/networknt/schema/RefValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static JsonSchema getRefSchema(JsonSchema parentSchema, ValidationContext valida
5757

5858
// This will determine the correct absolute uri for the refUri. This decision will take into
5959
// account the current uri of the parent schema.
60-
URI schemaUri = determineSchemaUri(validationContext.getJsonSchemaFactory().getURIFactory(), parentSchema, refUri);
60+
URI schemaUri = determineSchemaUri(validationContext.getURIFactory(), parentSchema, refUri);
6161
if (schemaUri == null) {
6262
return null;
6363
}

src/main/java/com/networknt/schema/ValidationContext.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@
1717
package com.networknt.schema;
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.networknt.schema.uri.URIFactory;
2021

2122
public class ValidationContext {
23+
private final URIFactory uriFactory;
2224
private final JsonMetaSchema metaSchema;
2325
private final JsonSchemaFactory jsonSchemaFactory;
2426
private SchemaValidatorsConfig config;
2527

26-
public ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory) {
28+
public ValidationContext(URIFactory uriFactory, JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory) {
29+
if (uriFactory == null) {
30+
throw new IllegalArgumentException("URIFactory must not be null");
31+
}
2732
if (metaSchema == null) {
2833
throw new IllegalArgumentException("JsonMetaSchema must not be null");
2934
}
3035
if (jsonSchemaFactory == null) {
3136
throw new IllegalArgumentException("JsonSchemaFactory must not be null");
3237
}
38+
this.uriFactory = uriFactory;
3339
this.metaSchema = metaSchema;
3440
this.jsonSchemaFactory = jsonSchemaFactory;
3541
}
@@ -39,6 +45,10 @@ public JsonValidator newValidator(String schemaPath, String keyword /* keyword *
3945
return metaSchema.newValidator(this, schemaPath, keyword, schemaNode, parentSchema);
4046
}
4147

48+
public URIFactory getURIFactory() {
49+
return this.uriFactory;
50+
}
51+
4252
public JsonSchemaFactory getJsonSchemaFactory() {
4353
return jsonSchemaFactory;
4454
}

src/main/java/com/networknt/schema/uri/URISchemeFetcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public Map<String, URIFetcher> getURIFetchers() {
4444
*/
4545
public InputStream fetch(final URI uri) throws IOException {
4646
final URIFetcher uriFetcher = this.uriFetchers.get(uri.getScheme());
47-
if (uriFetcher == null) {
47+
if (uriFetcher == null) {
4848
throw new IllegalArgumentException(String.format("Unsupported URI scheme encountered: %s", uri.getScheme()));
49-
}
49+
}
5050
return uriFetcher.fetch(uri);
5151
}
5252
}

0 commit comments

Comments
 (0)