Skip to content

Commit 45a8084

Browse files
authored
Merge pull request #167 from jawaff/fix/#166-uri-support
Fix/#166 uri support
2 parents fbae1d6 + 5f541d4 commit 45a8084

37 files changed

+680
-289
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ dist/
1616

1717
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1818
hs_err_pid*
19+
/.gradle/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public AdditionalPropertiesValidator(String schemaPath, JsonNode schemaNode, Jso
4747
additionalPropertiesSchema = null;
4848
} else if (schemaNode.isObject()) {
4949
allowAdditionalProperties = true;
50-
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUrl(), schemaNode, parentSchema);
50+
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
5151
} else {
5252
allowAdditionalProperties = false;
5353
additionalPropertiesSchema = null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
3535
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
3636
int size = schemaNode.size();
3737
for (int i = 0; i < size; i++) {
38-
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUrl(), schemaNode.get(i), parentSchema));
38+
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema));
3939
}
4040
}
4141

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import org.apache.commons.lang3.StringUtils;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
23-
2419
import java.util.ArrayList;
2520
import java.util.Collections;
2621
import java.util.LinkedHashSet;
2722
import java.util.List;
2823
import java.util.Set;
2924

25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import com.fasterxml.jackson.databind.JsonNode;
29+
3030
public class AnyOfValidator extends BaseJsonValidator implements JsonValidator {
3131
private static final Logger logger = LoggerFactory.getLogger(RequiredValidator.class);
3232

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

@@ -64,7 +64,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6464
allErrors.addAll(errors);
6565
}
6666
if (!expectedTypeList.isEmpty()) {
67-
return Collections.singleton(buildValidationMessage(at, StringUtils.join(expectedTypeList)));
67+
return Collections.singleton(buildValidationMessage(at, expectedTypeList.toArray(new String[expectedTypeList.size()])));
6868
}
6969
return Collections.unmodifiableSet(allErrors);
7070
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.networknt.schema.url.URLFactory;
19+
import java.net.URI;
20+
import java.util.Set;
21+
2122
import org.apache.commons.lang3.StringUtils;
2223
import org.slf4j.Logger;
2324

24-
import java.net.MalformedURLException;
25-
import java.net.URL;
26-
import java.util.Set;
25+
import com.fasterxml.jackson.databind.JsonNode;
2726

2827
public abstract class BaseJsonValidator implements JsonValidator {
2928
private String schemaPath;
@@ -71,22 +70,23 @@ protected JsonSchema fetchSubSchemaNode(ValidationContext validationContext) {
7170
}
7271

7372

74-
private static JsonSchema obtainSubSchemaNode(JsonNode schemaNode, ValidationContext validationContext){
75-
JsonNode node = schemaNode.get("id");
73+
private static JsonSchema obtainSubSchemaNode(final JsonNode schemaNode, final ValidationContext validationContext){
74+
final JsonNode node = schemaNode.get("id");
7675
if(node == null) return null;
7776
if(node.equals(schemaNode.get("$schema"))) return null;
7877

79-
try {
80-
String text = node.textValue();
81-
if (text == null) {
78+
final String text = node.textValue();
79+
if (text == null) {
80+
return null;
81+
}
82+
else {
83+
final URI uri;
84+
try {
85+
uri = validationContext.getJsonSchemaFactory().getURIFactory().create(node.textValue());
86+
} catch (IllegalArgumentException e) {
8287
return null;
8388
}
84-
else {
85-
URL url = URLFactory.toURL(node.textValue());
86-
return validationContext.getJsonSchemaFactory().getSchema(url, validationContext.getConfig());
87-
}
88-
} catch (MalformedURLException e) {
89-
return null;
89+
return validationContext.getJsonSchemaFactory().getSchema(uri, validationContext.getConfig());
9090
}
9191
}
9292

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public DependenciesValidator(String schemaPath, JsonNode schemaNode, JsonSchema
4444
depsProps.add(pvalue.get(i).asText());
4545
}
4646
} else if (pvalue.isObject()) {
47-
schemaDeps.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUrl(), pvalue, parentSchema));
47+
schemaDeps.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUri(), pvalue, parentSchema));
4848
}
4949
}
5050

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ public class ItemsValidator extends BaseJsonValidator implements JsonValidator {
3838
public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3939
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ITEMS, validationContext);
4040
if (schemaNode.isObject()) {
41-
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUrl(), schemaNode, parentSchema);
41+
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
4242
} else {
4343
tupleSchema = new ArrayList<JsonSchema>();
4444
for (JsonNode s : schemaNode) {
45-
tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUrl(), s, parentSchema));
45+
tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), s, parentSchema));
4646
}
4747

4848
JsonNode addItemNode = getParentSchema().getSchemaNode().get(PROPERTY_ADDITIONAL_ITEMS);
4949
if (addItemNode != null) {
5050
if (addItemNode.isBoolean()) {
5151
additionalItems = addItemNode.asBoolean();
5252
} else if (addItemNode.isObject()) {
53-
additionalSchema = new JsonSchema(validationContext, parentSchema.getCurrentUrl(), addItemNode);
53+
additionalSchema = new JsonSchema(validationContext, parentSchema.getCurrentUri(), addItemNode);
5454
}
5555
}
5656
}

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

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
package com.networknt.schema;
1818

1919
import java.io.UnsupportedEncodingException;
20-
import java.net.MalformedURLException;
21-
import java.net.URL;
20+
import java.net.URI;
2221
import java.net.URLDecoder;
2322
import java.util.Collections;
2423
import java.util.HashMap;
@@ -30,7 +29,6 @@
3029
import java.util.regex.Pattern;
3130

3231
import com.fasterxml.jackson.databind.JsonNode;
33-
import com.networknt.schema.url.URLFactory;
3432

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

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

5654
private JsonValidator requiredValidator = null;
5755

58-
public JsonSchema(ValidationContext validationContext, URL baseUrl, JsonNode schemaNode) {
59-
this(validationContext, "#", baseUrl, schemaNode, null);
56+
public JsonSchema(ValidationContext validationContext, URI baseUri, JsonNode schemaNode) {
57+
this(validationContext, "#", baseUri, schemaNode, null);
6058
}
6159

62-
public JsonSchema(ValidationContext validationContext, String schemaPath, URL currentUrl, JsonNode schemaNode,
60+
public JsonSchema(ValidationContext validationContext, String schemaPath, URI currentUri, JsonNode schemaNode,
6361
JsonSchema parent) {
64-
this(validationContext, schemaPath, currentUrl, schemaNode, parent, false);
62+
this(validationContext, schemaPath, currentUri, schemaNode, parent, false);
6563
}
6664

67-
public JsonSchema(ValidationContext validationContext, URL baseUrl, JsonNode schemaNode, boolean suppressSubSchemaRetrieval) {
68-
this(validationContext, "#", baseUrl, schemaNode, null, suppressSubSchemaRetrieval);
65+
public JsonSchema(ValidationContext validationContext, URI baseUri, JsonNode schemaNode, boolean suppressSubSchemaRetrieval) {
66+
this(validationContext, "#", baseUri, schemaNode, null, suppressSubSchemaRetrieval);
6967
}
7068

71-
private JsonSchema(ValidationContext validationContext, String schemaPath, URL currentUrl, JsonNode schemaNode,
69+
private JsonSchema(ValidationContext validationContext, String schemaPath, URI currentUri, JsonNode schemaNode,
7270
JsonSchema parent, boolean suppressSubSchemaRetrieval) {
7371
super(schemaPath, schemaNode, parent, null, suppressSubSchemaRetrieval);
7472
this.validationContext = validationContext;
7573
this.config = validationContext.getConfig();
76-
this.currentUrl = this.combineCurrentUrlWithIds(currentUrl, schemaNode);
74+
this.currentUri = this.combineCurrentUriWithIds(currentUri, schemaNode);
7775
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
7876
}
7977

80-
private URL combineCurrentUrlWithIds(URL currentUrl, JsonNode schemaNode) {
78+
private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) {
8179
final JsonNode idNode = schemaNode.get("id");
8280
if (idNode == null) {
83-
return currentUrl;
81+
return currentUri;
8482
} else {
85-
try
86-
{
87-
return URLFactory.toURL(currentUrl, idNode.asText());
88-
}
89-
catch (MalformedURLException e)
90-
{
91-
throw new IllegalArgumentException(String.format("Invalid 'id' in schema: %s", schemaNode), e);
83+
try {
84+
return this.validationContext.getJsonSchemaFactory().getURIFactory().create(currentUri, idNode.asText());
85+
} catch (IllegalArgumentException e) {
86+
throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.ID.getValue(), ValidatorTypeCode.ID, idNode.asText(), currentUri.toString()));
9287
}
9388
}
9489
}
9590

96-
public URL getCurrentUrl()
91+
public URI getCurrentUri()
9792
{
98-
return this.currentUrl;
93+
return this.currentUri;
9994
}
10095

10196
/**

0 commit comments

Comments
 (0)