Skip to content

Commit fe7ceea

Browse files
authored
Merge branch 'master' into optimized-oneof
2 parents d8d5019 + 3d52d67 commit fe7ceea

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,24 @@ public abstract class BaseJsonValidator implements JsonValidator {
2929
private String schemaPath;
3030
private JsonNode schemaNode;
3131
private JsonSchema parentSchema;
32-
private JsonSchema subSchema;
32+
private boolean suppressSubSchemaRetrieval;
3333
private ValidatorTypeCode validatorType;
3434
private ErrorMessageType errorMessageType;
3535

36+
3637
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
3738
ValidatorTypeCode validatorType, ValidationContext validationContext) {
38-
this(schemaPath, schemaNode, parentSchema, validatorType, obainSubSchemaNode(schemaNode, validationContext) );
39+
this(schemaPath, schemaNode, parentSchema, validatorType, false );
3940
}
4041

4142
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
42-
ValidatorTypeCode validatorType, JsonSchema subSchema) {
43+
ValidatorTypeCode validatorType, boolean suppressSubSchemaRetrieval) {
4344
this.errorMessageType = validatorType;
4445
this.schemaPath = schemaPath;
4546
this.schemaNode = schemaNode;
4647
this.parentSchema = parentSchema;
4748
this.validatorType = validatorType;
48-
this.subSchema = subSchema;
49+
this.suppressSubSchemaRetrieval = suppressSubSchemaRetrieval;
4950
}
5051

5152
protected String getSchemaPath() {
@@ -60,15 +61,12 @@ protected JsonSchema getParentSchema() {
6061
return parentSchema;
6162
}
6263

63-
protected JsonSchema getSubSchema() {
64-
return subSchema;
64+
protected JsonSchema fetchSubSchemaNode(ValidationContext validationContext) {
65+
return suppressSubSchemaRetrieval ? null : obtainSubSchemaNode(schemaNode, validationContext);
6566
}
6667

67-
protected boolean hasSubSchema() {
68-
return subSchema != null;
69-
}
7068

71-
protected static JsonSchema obainSubSchemaNode(JsonNode schemaNode, ValidationContext validationContext){
69+
private static JsonSchema obtainSubSchemaNode(JsonNode schemaNode, ValidationContext validationContext){
7270
JsonNode node = schemaNode.get("id");
7371
if(node == null) return null;
7472
if(node.equals(schemaNode.get("$schema"))) return null;

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ public class JsonSchema extends BaseJsonValidator {
4646

4747
JsonSchema(ValidationContext validationContext, String schemaPath, JsonNode schemaNode,
4848
JsonSchema parent) {
49-
this(validationContext, schemaPath, schemaNode, parent, obainSubSchemaNode(schemaNode, validationContext));
49+
this(validationContext, schemaPath, schemaNode, parent, false);
5050
}
5151

5252
JsonSchema(ValidationContext validatorFactory, String schemaPath, JsonNode schemaNode,
53-
JsonSchema parent, JsonSchema subSchema) {
54-
super(schemaPath, schemaNode, parent, null, subSchema);
53+
JsonSchema parent, boolean suppressSubSchemaRetrieval) {
54+
super(schemaPath, schemaNode, parent, null, suppressSubSchemaRetrieval);
5555
this.validationContext = validatorFactory;
5656
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
5757
}
5858

59-
public JsonSchema(ValidationContext validationContext, JsonNode schemaNode, JsonSchema subSchema) {
60-
this(validationContext, "#", schemaNode, null, subSchema);
59+
public JsonSchema(ValidationContext validationContext, JsonNode schemaNode, boolean suppressSubSchemaRetrieval) {
60+
this(validationContext, "#", schemaNode, null, suppressSubSchemaRetrieval);
6161
}
6262

6363
/**
@@ -84,8 +84,11 @@ public JsonNode getRefSchemaNode(String ref) {
8484
} else {
8585
node = node.get(key);
8686
}
87-
if (node == null && schema.hasSubSchema()){
88-
node = schema.getSubSchema().getRefSchemaNode(ref);
87+
if (node == null){
88+
JsonSchema subSchema = schema.fetchSubSchemaNode(validationContext);
89+
if (subSchema != null) {
90+
node = subSchema.getRefSchemaNode(ref);
91+
}
8992
}
9093
if (node == null){
9194
break;

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

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

1717
package com.networknt.schema;
1818

19+
import java.util.Collections;
20+
import java.util.Set;
21+
1922
public class JsonSchemaException extends RuntimeException {
2023
private static final long serialVersionUID = -7805792737596582110L;
24+
private ValidationMessage validationMessage;
2125

2226
public JsonSchemaException(ValidationMessage validationMessage) {
2327
super(validationMessage.getMessage());
28+
this.validationMessage = validationMessage;
2429
}
2530

2631
public JsonSchemaException(String message) {
@@ -31,4 +36,10 @@ public JsonSchemaException(Throwable throwable) {
3136
super(throwable);
3237
}
3338

39+
public Set<ValidationMessage> getValidationMessages() {
40+
if (validationMessage == null) {
41+
return Collections.emptySet();
42+
}
43+
return Collections.singleton(validationMessage);
44+
}
3445
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public JsonSchema getSchema(URL schemaURL) {
189189

190190
if (idMatchesSourceUrl(jsonMetaSchema, schemaNode, schemaURL)) {
191191

192-
return new JsonSchema(new ValidationContext(jsonMetaSchema, this), schemaNode, null);
192+
return new JsonSchema(new ValidationContext(jsonMetaSchema, this), schemaNode, true /*retrieved via id, resolving will not change anything*/);
193193
}
194194

195195
return newJsonSchema(schemaNode);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.networknt.schema.url.URLFactory;
21-
import org.slf4j.Logger;
22-
import org.slf4j.LoggerFactory;
23-
2419
import java.io.InputStream;
2520
import java.net.MalformedURLException;
2621
import java.net.URL;
2722
import java.text.MessageFormat;
2823
import java.util.Collections;
2924
import java.util.Set;
3025

26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import com.fasterxml.jackson.databind.JsonNode;
30+
import com.networknt.schema.url.URLFactory;
31+
3132
public class RefValidator extends BaseJsonValidator implements JsonValidator {
3233
private static final Logger logger = LoggerFactory.getLogger(RefValidator.class);
3334

0 commit comments

Comments
 (0)