Skip to content

fix/#166 uri support update #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/BaseJsonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private static JsonSchema obtainSubSchemaNode(final JsonNode schemaNode, final V
else {
final URI uri;
try {
uri = validationContext.getJsonSchemaFactory().getURIFactory().create(node.textValue());
uri = validationContext.getURIFactory().create(node.textValue());
} catch (IllegalArgumentException e) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) {
return currentUri;
} else {
try {
return this.validationContext.getJsonSchemaFactory().getURIFactory().create(currentUri, idNode.asText());
return this.validationContext.getURIFactory().create(currentUri, idNode.asText());
} catch (IllegalArgumentException e) {
throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.ID.getValue(), ValidatorTypeCode.ID, idNode.asText(), currentUri.toString()));
}
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/com/networknt/schema/JsonSchemaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private JsonSchema newJsonSchema(final URI schemaUri, final JsonNode schemaNode,

protected ValidationContext createValidationContext(final JsonNode schemaNode) {
final JsonMetaSchema jsonMetaSchema = findMetaSchemaForSchema(schemaNode);
return new ValidationContext(jsonMetaSchema, this);
return new ValidationContext(this.uriFactory, jsonMetaSchema, this);
}

private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
Expand All @@ -246,10 +246,6 @@ private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
return jsonMetaSchema;
}

public URIFactory getURIFactory() {
return this.uriFactory;
}

public JsonSchema getSchema(final String schema, final SchemaValidatorsConfig config) {
try {
final JsonNode schemaNode = mapper.readTree(schema);
Expand Down Expand Up @@ -286,7 +282,7 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co

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

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

return newJsonSchema(mappedUri, schemaNode, config);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/RefValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static JsonSchema getRefSchema(JsonSchema parentSchema, ValidationContext valida

// This will determine the correct absolute uri for the refUri. This decision will take into
// account the current uri of the parent schema.
URI schemaUri = determineSchemaUri(validationContext.getJsonSchemaFactory().getURIFactory(), parentSchema, refUri);
URI schemaUri = determineSchemaUri(validationContext.getURIFactory(), parentSchema, refUri);
if (schemaUri == null) {
return null;
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/networknt/schema/ValidationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.uri.URIFactory;

public class ValidationContext {
private final URIFactory uriFactory;
private final JsonMetaSchema metaSchema;
private final JsonSchemaFactory jsonSchemaFactory;
private SchemaValidatorsConfig config;

public ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory) {
public ValidationContext(URIFactory uriFactory, JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory) {
if (uriFactory == null) {
throw new IllegalArgumentException("URIFactory must not be null");
}
if (metaSchema == null) {
throw new IllegalArgumentException("JsonMetaSchema must not be null");
}
if (jsonSchemaFactory == null) {
throw new IllegalArgumentException("JsonSchemaFactory must not be null");
}
this.uriFactory = uriFactory;
this.metaSchema = metaSchema;
this.jsonSchemaFactory = jsonSchemaFactory;
}
Expand All @@ -39,6 +45,10 @@ public JsonValidator newValidator(String schemaPath, String keyword /* keyword *
return metaSchema.newValidator(this, schemaPath, keyword, schemaNode, parentSchema);
}

public URIFactory getURIFactory() {
return this.uriFactory;
}

public JsonSchemaFactory getJsonSchemaFactory() {
return jsonSchemaFactory;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/networknt/schema/uri/URISchemeFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public Map<String, URIFetcher> getURIFetchers() {
*/
public InputStream fetch(final URI uri) throws IOException {
final URIFetcher uriFetcher = this.uriFetchers.get(uri.getScheme());
if (uriFetcher == null) {
if (uriFetcher == null) {
throw new IllegalArgumentException(String.format("Unsupported URI scheme encountered: %s", uri.getScheme()));
}
}
return uriFetcher.fetch(uri);
}
}