Skip to content

Commit

Permalink
Updated URL variables to URIs. Also created a URIFactory interface th…
Browse files Browse the repository at this point in the history
…at needs to be used instead of URI#resolve and URI#create.
  • Loading branch information
jawaff committed Jun 23, 2019
1 parent fbae1d6 commit 53bf363
Show file tree
Hide file tree
Showing 26 changed files with 298 additions and 349 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,3 +16,4 @@ dist/

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/.gradle/
Expand Up @@ -47,7 +47,7 @@ public AdditionalPropertiesValidator(String schemaPath, JsonNode schemaNode, Jso
additionalPropertiesSchema = null;
} else if (schemaNode.isObject()) {
allowAdditionalProperties = true;
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUrl(), schemaNode, parentSchema);
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
} else {
allowAdditionalProperties = false;
additionalPropertiesSchema = null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/AllOfValidator.java
Expand Up @@ -35,7 +35,7 @@ public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
int size = schemaNode.size();
for (int i = 0; i < size; i++) {
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUrl(), schemaNode.get(i), parentSchema));
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema));
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/networknt/schema/AnyOfValidator.java
Expand Up @@ -16,17 +16,17 @@

package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;

public class AnyOfValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(RequiredValidator.class);

Expand All @@ -36,7 +36,7 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF, validationContext);
int size = schemaNode.size();
for (int i = 0; i < size; i++) {
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUrl(), schemaNode.get(i), parentSchema));
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema));
}
}

Expand Down Expand Up @@ -64,7 +64,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
allErrors.addAll(errors);
}
if (!expectedTypeList.isEmpty()) {
return Collections.singleton(buildValidationMessage(at, StringUtils.join(expectedTypeList)));
return Collections.singleton(buildValidationMessage(at, expectedTypeList.toArray(new String[expectedTypeList.size()])));
}
return Collections.unmodifiableSet(allErrors);
}
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/com/networknt/schema/BaseJsonValidator.java
Expand Up @@ -16,14 +16,13 @@

package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.url.URLFactory;
import java.net.URI;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import com.fasterxml.jackson.databind.JsonNode;

public abstract class BaseJsonValidator implements JsonValidator {
private String schemaPath;
Expand Down Expand Up @@ -71,22 +70,23 @@ protected JsonSchema fetchSubSchemaNode(ValidationContext validationContext) {
}


private static JsonSchema obtainSubSchemaNode(JsonNode schemaNode, ValidationContext validationContext){
JsonNode node = schemaNode.get("id");
private static JsonSchema obtainSubSchemaNode(final JsonNode schemaNode, final ValidationContext validationContext){
final JsonNode node = schemaNode.get("id");
if(node == null) return null;
if(node.equals(schemaNode.get("$schema"))) return null;

try {
String text = node.textValue();
if (text == null) {
final String text = node.textValue();
if (text == null) {
return null;
}
else {
final URI uri;
try {
uri = URI.create(node.textValue());
} catch (IllegalArgumentException e) {
return null;
}
else {
URL url = URLFactory.toURL(node.textValue());
return validationContext.getJsonSchemaFactory().getSchema(url, validationContext.getConfig());
}
} catch (MalformedURLException e) {
return null;
return validationContext.getJsonSchemaFactory().getSchema(uri, validationContext.getConfig());
}
}

Expand Down
Expand Up @@ -44,7 +44,7 @@ public DependenciesValidator(String schemaPath, JsonNode schemaNode, JsonSchema
depsProps.add(pvalue.get(i).asText());
}
} else if (pvalue.isObject()) {
schemaDeps.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUrl(), pvalue, parentSchema));
schemaDeps.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUri(), pvalue, parentSchema));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/networknt/schema/ItemsValidator.java
Expand Up @@ -38,19 +38,19 @@ public class ItemsValidator extends BaseJsonValidator implements JsonValidator {
public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ITEMS, validationContext);
if (schemaNode.isObject()) {
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUrl(), schemaNode, parentSchema);
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
} else {
tupleSchema = new ArrayList<JsonSchema>();
for (JsonNode s : schemaNode) {
tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUrl(), s, parentSchema));
tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), s, parentSchema));
}

JsonNode addItemNode = getParentSchema().getSchemaNode().get(PROPERTY_ADDITIONAL_ITEMS);
if (addItemNode != null) {
if (addItemNode.isBoolean()) {
additionalItems = addItemNode.asBoolean();
} else if (addItemNode.isObject()) {
additionalSchema = new JsonSchema(validationContext, parentSchema.getCurrentUrl(), addItemNode);
additionalSchema = new JsonSchema(validationContext, parentSchema.getCurrentUri(), addItemNode);
}
}
}
Expand Down
55 changes: 25 additions & 30 deletions src/main/java/com/networknt/schema/JsonSchema.java
Expand Up @@ -17,8 +17,7 @@
package com.networknt.schema;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -30,7 +29,6 @@
import java.util.regex.Pattern;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.url.URLFactory;

/**
* This is the core of json constraint implementation. It parses json constraint
Expand All @@ -43,59 +41,56 @@ public class JsonSchema extends BaseJsonValidator {
private final ValidationContext validationContext;

/**
* This is the current url of this schema. This url could refer to the url of this schema's file
* or it could potentially be a url that has been altered by an id. An 'id' is able to completely overwrite
* the current url or add onto it. This is necessary so that '$ref's are able to be relative to a
* combination of the current schema file's url and 'id' urls visible to this schema.
* This is the current uri of this schema. This uri could refer to the uri of this schema's file
* or it could potentially be a uri that has been altered by an id. An 'id' is able to completely overwrite
* the current uri or add onto it. This is necessary so that '$ref's are able to be relative to a
* combination of the current schema file's uri and 'id' uris visible to this schema.
*
* This can be null. If it is null, then the creation of relative urls will fail. However, an absolute
* 'id' would still be able to specify an absolute url.
* This can be null. If it is null, then the creation of relative uris will fail. However, an absolute
* 'id' would still be able to specify an absolute uri.
*/
private final URL currentUrl;
private final URI currentUri;

private JsonValidator requiredValidator = null;

public JsonSchema(ValidationContext validationContext, URL baseUrl, JsonNode schemaNode) {
this(validationContext, "#", baseUrl, schemaNode, null);
public JsonSchema(ValidationContext validationContext, URI baseUri, JsonNode schemaNode) {
this(validationContext, "#", baseUri, schemaNode, null);
}

public JsonSchema(ValidationContext validationContext, String schemaPath, URL currentUrl, JsonNode schemaNode,
public JsonSchema(ValidationContext validationContext, String schemaPath, URI currentUri, JsonNode schemaNode,
JsonSchema parent) {
this(validationContext, schemaPath, currentUrl, schemaNode, parent, false);
this(validationContext, schemaPath, currentUri, schemaNode, parent, false);
}

public JsonSchema(ValidationContext validationContext, URL baseUrl, JsonNode schemaNode, boolean suppressSubSchemaRetrieval) {
this(validationContext, "#", baseUrl, schemaNode, null, suppressSubSchemaRetrieval);
public JsonSchema(ValidationContext validationContext, URI baseUri, JsonNode schemaNode, boolean suppressSubSchemaRetrieval) {
this(validationContext, "#", baseUri, schemaNode, null, suppressSubSchemaRetrieval);
}

private JsonSchema(ValidationContext validationContext, String schemaPath, URL currentUrl, JsonNode schemaNode,
private JsonSchema(ValidationContext validationContext, String schemaPath, URI currentUri, JsonNode schemaNode,
JsonSchema parent, boolean suppressSubSchemaRetrieval) {
super(schemaPath, schemaNode, parent, null, suppressSubSchemaRetrieval);
this.validationContext = validationContext;
this.config = validationContext.getConfig();
this.currentUrl = this.combineCurrentUrlWithIds(currentUrl, schemaNode);
this.currentUri = this.combineCurrentUriWithIds(currentUri, schemaNode);
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
}

private URL combineCurrentUrlWithIds(URL currentUrl, JsonNode schemaNode) {
private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) {
final JsonNode idNode = schemaNode.get("id");
if (idNode == null) {
return currentUrl;
return currentUri;
} else {
try
{
return URLFactory.toURL(currentUrl, idNode.asText());
}
catch (MalformedURLException e)
{
throw new IllegalArgumentException(String.format("Invalid 'id' in schema: %s", schemaNode), e);
}
try {
return currentUri.resolve(idNode.asText());
} catch (IllegalArgumentException e) {
throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.ID.getValue(), ValidatorTypeCode.ID, idNode.asText(), currentUri.toString()));
}
}
}

public URL getCurrentUrl()
public URI getCurrentUri()
{
return this.currentUrl;
return this.currentUri;
}

/**
Expand Down

0 comments on commit 53bf363

Please sign in to comment.