Skip to content

Resolve sub schema node only if really needed #51

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Dec 10, 2017
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
18 changes: 8 additions & 10 deletions src/main/java/com/networknt/schema/BaseJsonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,24 @@ public abstract class BaseJsonValidator implements JsonValidator {
private String schemaPath;
private JsonNode schemaNode;
private JsonSchema parentSchema;
private JsonSchema subSchema;
private boolean suppressSubSchemaRetrieval;
private ValidatorTypeCode validatorType;
private ErrorMessageType errorMessageType;


public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
ValidatorTypeCode validatorType, ValidationContext validationContext) {
this(schemaPath, schemaNode, parentSchema, validatorType, obainSubSchemaNode(schemaNode, validationContext) );
this(schemaPath, schemaNode, parentSchema, validatorType, false );
}

public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
ValidatorTypeCode validatorType, JsonSchema subSchema) {
ValidatorTypeCode validatorType, boolean suppressSubSchemaRetrieval) {
this.errorMessageType = validatorType;
this.schemaPath = schemaPath;
this.schemaNode = schemaNode;
this.parentSchema = parentSchema;
this.validatorType = validatorType;
this.subSchema = subSchema;
this.suppressSubSchemaRetrieval = suppressSubSchemaRetrieval;
}

protected String getSchemaPath() {
Expand All @@ -60,15 +61,12 @@ protected JsonSchema getParentSchema() {
return parentSchema;
}

protected JsonSchema getSubSchema() {
return subSchema;
protected JsonSchema fetchSubSchemaNode(ValidationContext validationContext) {
return suppressSubSchemaRetrieval ? null : obtainSubSchemaNode(schemaNode, validationContext);
}

protected boolean hasSubSchema() {
return subSchema != null;
}

protected static JsonSchema obainSubSchemaNode(JsonNode schemaNode, ValidationContext validationContext){
private static JsonSchema obtainSubSchemaNode(JsonNode schemaNode, ValidationContext validationContext){
JsonNode node = schemaNode.get("id");
if(node == null) return null;
if(node.equals(schemaNode.get("$schema"))) return null;
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/com/networknt/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ public class JsonSchema extends BaseJsonValidator {

JsonSchema(ValidationContext validationContext, String schemaPath, JsonNode schemaNode,
JsonSchema parent) {
this(validationContext, schemaPath, schemaNode, parent, obainSubSchemaNode(schemaNode, validationContext));
this(validationContext, schemaPath, schemaNode, parent, false);
}

JsonSchema(ValidationContext validatorFactory, String schemaPath, JsonNode schemaNode,
JsonSchema parent, JsonSchema subSchema) {
super(schemaPath, schemaNode, parent, null, subSchema);
JsonSchema parent, boolean suppressSubSchemaRetrieval) {
super(schemaPath, schemaNode, parent, null, suppressSubSchemaRetrieval);
this.validationContext = validatorFactory;
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
}

public JsonSchema(ValidationContext validationContext, JsonNode schemaNode, JsonSchema subSchema) {
this(validationContext, "#", schemaNode, null, subSchema);
public JsonSchema(ValidationContext validationContext, JsonNode schemaNode, boolean suppressSubSchemaRetrieval) {
this(validationContext, "#", schemaNode, null, suppressSubSchemaRetrieval);
}

/**
Expand All @@ -84,8 +84,11 @@ public JsonNode getRefSchemaNode(String ref) {
} else {
node = node.get(key);
}
if (node == null && schema.hasSubSchema()){
node = schema.getSubSchema().getRefSchemaNode(ref);
if (node == null){
JsonSchema subSchema = schema.fetchSubSchemaNode(validationContext);
if (subSchema != null) {
node = subSchema.getRefSchemaNode(ref);
}
}
if (node == null){
break;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/JsonSchemaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public JsonSchema getSchema(URL schemaURL) {

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

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

return newJsonSchema(schemaNode);
Expand Down