Skip to content

Commit 85d642b

Browse files
authored
Fix issues with hierarchy output report (#947)
* Fix anyOf short circuit behavior when annotation collection is on * Fixes for the hierarchical output due to the evaluation path and instance location * Refactor * Fix propertyNames messages * Fix min max contains messages * Fix hierarchical report * Add test for propertyNames * Fix additionalItems * Fix additionalItems * Refactor * Add test * Fix * Revert due to encoding * Fix * Fix * Fix boolean reason * add to schema ref * Fix walk runners * Fix * Add option to disable preload * refactor * optimise fail fast * Allow fail fast to drop oneOf child errors * Remove unused field * Fix message for unevaluatedProperties on fail fast * fix messages
1 parent 91385c2 commit 85d642b

File tree

105 files changed

+1237
-532
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+1237
-532
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
113113
errors = new LinkedHashSet<>();
114114
}
115115
errors.add(message().instanceNode(node).property(pname)
116-
.instanceLocation(instanceLocation.append(pname))
116+
.instanceLocation(instanceLocation)
117117
.locale(executionContext.getExecutionConfig().getLocale())
118-
.failFast(executionContext.getExecutionConfig().isFailFast()).arguments(pname).build());
118+
.failFast(executionContext.isFailFast()).arguments(pname).build());
119119
} else {
120120
if (additionalPropertiesSchema != null) {
121121
ValidatorState state = executionContext.getValidatorState();

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
6969
int numberOfValidSubSchemas = 0;
7070
try {
7171
// Save flag as nested schema evaluation shouldn't trigger fail fast
72-
boolean failFast = executionContext.getExecutionConfig().isFailFast();
72+
boolean failFast = executionContext.isFailFast();
7373
try {
74-
executionContext.getExecutionConfig().setFailFast(false);
74+
executionContext.setFailFast(false);
7575
for (JsonSchema schema : this.schemas) {
7676
Set<ValidationMessage> errors = Collections.emptySet();
7777
state.setMatchedNode(initialHasMatchedNode);
@@ -104,7 +104,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
104104
}
105105

106106
if (errors.isEmpty() && (!this.validationContext.getConfig().isOpenAPI3StyleDiscriminators())
107-
&& canShortCircuit()) {
107+
&& canShortCircuit() && canShortCircuit(executionContext)) {
108108
// Clear all errors.
109109
allErrors.clear();
110110
// return empty errors.
@@ -115,7 +115,7 @@ && canShortCircuit()) {
115115
allErrors.addAll(errors);
116116
allErrors.add(message().instanceNode(node).instanceLocation(instanceLocation)
117117
.locale(executionContext.getExecutionConfig().getLocale())
118-
.failFast(executionContext.getExecutionConfig().isFailFast())
118+
.failFast(executionContext.isFailFast())
119119
.arguments(DISCRIMINATOR_REMARK).build());
120120
} else {
121121
// Clear all errors.
@@ -128,7 +128,7 @@ && canShortCircuit()) {
128128
}
129129
} finally {
130130
// Restore flag
131-
executionContext.getExecutionConfig().setFailFast(failFast);
131+
executionContext.setFailFast(failFast);
132132
}
133133

134134
// determine only those errors which are NOT of type "required" property missing
@@ -174,7 +174,24 @@ public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode n
174174
}
175175
return new LinkedHashSet<>();
176176
}
177-
177+
178+
/**
179+
* If annotation collection is enabled cannot short circuit.
180+
*
181+
* @see <a href=
182+
* "https://github.com/json-schema-org/json-schema-spec/blob/f8967bcbc6cee27753046f63024b55336a9b1b54/jsonschema-core.md?plain=1#L1717-L1720">anyOf</a>
183+
* @param executionContext the execution context
184+
* @return true if can short circuit
185+
*/
186+
protected boolean canShortCircuit(ExecutionContext executionContext) {
187+
return !executionContext.getExecutionConfig().isAnnotationCollectionEnabled();
188+
}
189+
190+
/**
191+
* If annotations are require for evaluation cannot short circuit.
192+
*
193+
* @return true if can short circuit
194+
*/
178195
protected boolean canShortCircuit() {
179196
if (this.canShortCircuit == null) {
180197
boolean canShortCircuit = true;

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
public abstract class BaseJsonValidator extends ValidationMessageHandler implements JsonValidator {
3333
protected final boolean suppressSubSchemaRetrieval;
34-
protected final ApplyDefaultsStrategy applyDefaultsStrategy;
35-
private final PathType pathType;
3634

3735
protected final JsonNode schemaNode;
3836

@@ -59,13 +57,6 @@ public BaseJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationP
5957
this.validationContext = validationContext;
6058
this.schemaNode = schemaNode;
6159
this.suppressSubSchemaRetrieval = suppressSubSchemaRetrieval;
62-
this.applyDefaultsStrategy = (validationContext != null && validationContext.getConfig() != null
63-
&& validationContext.getConfig().getApplyDefaultsStrategy() != null)
64-
? validationContext.getConfig().getApplyDefaultsStrategy()
65-
: ApplyDefaultsStrategy.EMPTY_APPLY_DEFAULTS_STRATEGY;
66-
this.pathType = (validationContext != null && validationContext.getConfig() != null
67-
&& validationContext.getConfig().getPathType() != null) ? validationContext.getConfig().getPathType()
68-
: PathType.DEFAULT;
6960
}
7061

7162
/**
@@ -76,8 +67,6 @@ public BaseJsonValidator(SchemaLocation schemaLocation, JsonNodePath evaluationP
7667
protected BaseJsonValidator(BaseJsonValidator copy) {
7768
super(copy);
7869
this.suppressSubSchemaRetrieval = copy.suppressSubSchemaRetrieval;
79-
this.applyDefaultsStrategy = copy.applyDefaultsStrategy;
80-
this.pathType = copy.pathType;
8170
this.schemaNode = copy.schemaNode;
8271
this.validationContext = copy.validationContext;
8372
}
@@ -307,7 +296,7 @@ protected void preloadJsonSchemas(final Collection<JsonSchema> schemas) {
307296
* @return The path.
308297
*/
309298
protected JsonNodePath atRoot() {
310-
return new JsonNodePath(this.pathType);
299+
return new JsonNodePath(this.validationContext.getConfig().getPathType());
311300
}
312301

313302
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
4141
if (schemaNode.decimalValue().compareTo(node.decimalValue()) != 0) {
4242
return Collections.singleton(message().instanceNode(node).instanceLocation(instanceLocation)
4343
.locale(executionContext.getExecutionConfig().getLocale())
44-
.failFast(executionContext.getExecutionConfig().isFailFast()).arguments(schemaNode.asText())
44+
.failFast(executionContext.isFailFast()).arguments(schemaNode.asText())
4545
.build());
4646
}
4747
} else if (!schemaNode.equals(node)) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
8080
List<Integer> indexes = new ArrayList<>(); // for the annotation
8181
if (null != this.schema && node.isArray()) {
8282
// Save flag as nested schema evaluation shouldn't trigger fail fast
83-
boolean failFast = executionContext.getExecutionConfig().isFailFast();
83+
boolean failFast = executionContext.isFailFast();
8484
try {
85-
executionContext.getExecutionConfig().setFailFast(false);
85+
executionContext.setFailFast(false);
8686
for (JsonNode n : node) {
8787
JsonNodePath path = instanceLocation.append(i);
8888
if (this.schema.validate(executionContext, n, rootNode, path).isEmpty()) {
@@ -93,7 +93,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
9393
}
9494
} finally {
9595
// Restore flag
96-
executionContext.getExecutionConfig().setFailFast(failFast);
96+
executionContext.setFailFast(failFast);
9797
}
9898
int m = 1; // default to 1 if "min" not specified
9999
if (this.min != null) {
@@ -102,13 +102,13 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
102102
if (actual < m) {
103103
results = boundsViolated(isMinV201909 ? ValidatorTypeCode.MIN_CONTAINS : ValidatorTypeCode.CONTAINS,
104104
executionContext.getExecutionConfig().getLocale(),
105-
executionContext.getExecutionConfig().isFailFast(), node, instanceLocation, m);
105+
executionContext.isFailFast(), node, instanceLocation, m);
106106
}
107107

108108
if (this.max != null && actual > this.max) {
109109
results = boundsViolated(isMinV201909 ? ValidatorTypeCode.MAX_CONTAINS : ValidatorTypeCode.CONTAINS,
110110
executionContext.getExecutionConfig().getLocale(),
111-
executionContext.getExecutionConfig().isFailFast(), node, instanceLocation, this.max);
111+
executionContext.isFailFast(), node, instanceLocation, this.max);
112112
}
113113
}
114114

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
8282
if (!matches(node.asText())) {
8383
return Collections.singleton(message().instanceNode(node).instanceLocation(instanceLocation)
8484
.locale(executionContext.getExecutionConfig().getLocale())
85-
.failFast(executionContext.getExecutionConfig().isFailFast()).arguments(this.contentEncoding)
85+
.failFast(executionContext.isFailFast()).arguments(this.contentEncoding)
8686
.build());
8787
}
8888
return Collections.emptySet();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
106106
if (!matches(node.asText())) {
107107
return Collections.singleton(message().instanceNode(node).instanceLocation(instanceLocation)
108108
.locale(executionContext.getExecutionConfig().getLocale())
109-
.failFast(executionContext.getExecutionConfig().isFailFast()).arguments(this.contentMediaType)
109+
.failFast(executionContext.isFailFast()).arguments(this.contentMediaType)
110110
.build());
111111
}
112112
return Collections.emptySet();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
7575
if (node.get(field) == null) {
7676
errors.add(message().instanceNode(node).property(pname).instanceLocation(instanceLocation)
7777
.locale(executionContext.getExecutionConfig().getLocale())
78-
.failFast(executionContext.getExecutionConfig().isFailFast())
78+
.failFast(executionContext.isFailFast())
7979
.arguments(propertyDeps.toString()).build());
8080
}
8181
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
5959
if (node.get(field) == null) {
6060
errors.add(message().instanceNode(node).property(pname).instanceLocation(instanceLocation)
6161
.locale(executionContext.getExecutionConfig().getLocale())
62-
.failFast(executionContext.getExecutionConfig().isFailFast()).arguments(field, pname)
62+
.failFast(executionContext.isFailFast()).arguments(field, pname)
6363
.build());
6464
}
6565
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNo
9292
if (!nodes.contains(node) && !( this.validationContext.getConfig().isTypeLoose() && isTypeLooseContainsInEnum(node))) {
9393
return Collections.singleton(message().instanceNode(node).instanceLocation(instanceLocation)
9494
.locale(executionContext.getExecutionConfig().getLocale())
95-
.failFast(executionContext.getExecutionConfig().isFailFast()).arguments(error).build());
95+
.failFast(executionContext.isFailFast()).arguments(error).build());
9696
}
9797

9898
return Collections.emptySet();

0 commit comments

Comments
 (0)