Skip to content

Commit a01f5d4

Browse files
authored
Merge pull request #51 from joblift/lazy-sub-schema-resolve
Resolve sub schema node only if really needed
2 parents 724a442 + 37368a5 commit a01f5d4

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
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/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);

0 commit comments

Comments
 (0)