Skip to content

Commit 8b71dac

Browse files
committed
Refactor set to list in execution context
1 parent 19b2207 commit 8b71dac

File tree

163 files changed

+800
-1072
lines changed

Some content is hidden

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

163 files changed

+800
-1072
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<groupId>com.networknt</groupId>
2525
<artifactId>json-schema-validator</artifactId>
26-
<version>1.5.9</version>
26+
<version>2.0.0-SNAPSHOT</version>
2727
<packaging>bundle</packaging>
2828
<name>JsonSchemaValidator</name>
2929
<description>A json schema validator that supports draft v4, v6, v7, v2019-09 and v2020-12</description>

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

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,17 @@ public AdditionalPropertiesValidator(SchemaLocation schemaLocation, JsonNodePath
8181
}
8282

8383
@Override
84-
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
84+
public void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
8585
JsonNodePath instanceLocation) {
86-
return validate(executionContext, node, rootNode, instanceLocation, false);
86+
validate(executionContext, node, rootNode, instanceLocation, false);
8787
}
8888

89-
protected Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
89+
protected void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
9090
JsonNodePath instanceLocation, boolean walk) {
9191
debug(logger, executionContext, node, rootNode, instanceLocation);
9292
if (!node.isObject()) {
9393
// ignore no object
94-
return Collections.emptySet();
94+
return;
9595
}
9696

9797
Set<String> matchedInstancePropertyNames = null;
@@ -108,8 +108,6 @@ protected Set<ValidationMessage> validate(ExecutionContext executionContext, Jso
108108
}
109109
}
110110

111-
Set<ValidationMessage> errors = null;
112-
113111
for (Iterator<Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) {
114112
Entry<String, JsonNode> entry = it.next();
115113
String pname = entry.getKey();
@@ -119,25 +117,18 @@ protected Set<ValidationMessage> validate(ExecutionContext executionContext, Jso
119117
}
120118
if (!allowedProperties.contains(pname) && !handledByPatternProperties(pname)) {
121119
if (!allowAdditionalProperties) {
122-
if (errors == null) {
123-
errors = new LinkedHashSet<>();
124-
}
125-
errors.add(message().instanceNode(node).property(pname)
120+
executionContext.addError(message().instanceNode(node).property(pname)
126121
.instanceLocation(instanceLocation)
127122
.locale(executionContext.getExecutionConfig().getLocale())
128123
.failFast(executionContext.isFailFast()).arguments(pname).build());
129124
} else {
130125
if (additionalPropertiesSchema != null) {
131-
Set<ValidationMessage> results = !walk
132-
? additionalPropertiesSchema.validate(executionContext, entry.getValue(), rootNode,
133-
instanceLocation.append(pname))
134-
: additionalPropertiesSchema.walk(executionContext, entry.getValue(), rootNode,
135-
instanceLocation.append(pname), true);
136-
if (!results.isEmpty()) {
137-
if (errors == null) {
138-
errors = new LinkedHashSet<>();
139-
}
140-
errors.addAll(results);
126+
if (!walk) {
127+
additionalPropertiesSchema.validate(executionContext, entry.getValue(), rootNode,
128+
instanceLocation.append(pname));
129+
} else {
130+
additionalPropertiesSchema.walk(executionContext, entry.getValue(), rootNode,
131+
instanceLocation.append(pname), true);
141132
}
142133
}
143134
}
@@ -149,18 +140,18 @@ protected Set<ValidationMessage> validate(ExecutionContext executionContext, Jso
149140
.value(matchedInstancePropertyNames != null ? matchedInstancePropertyNames : Collections.emptySet())
150141
.build());
151142
}
152-
return errors == null ? Collections.emptySet() : Collections.unmodifiableSet(errors);
153143
}
154144

155145
@Override
156-
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
146+
public void walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
157147
if (shouldValidateSchema && node != null) {
158-
return validate(executionContext, node, rootNode, instanceLocation, true);
148+
validate(executionContext, node, rootNode, instanceLocation, true);
149+
return;
159150
}
160151

161152
if (node == null || !node.isObject()) {
162153
// ignore no object
163-
return Collections.emptySet();
154+
return;
164155
}
165156

166157
// Else continue walking.
@@ -179,7 +170,6 @@ public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode n
179170
}
180171
}
181172
}
182-
return Collections.emptySet();
183173
}
184174

185175
private boolean handledByPatternProperties(String pname) {

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

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.fasterxml.jackson.databind.JsonNode;
2222
import com.fasterxml.jackson.databind.node.ObjectNode;
23-
import com.networknt.schema.utils.SetView;
2423

2524
import org.slf4j.Logger;
2625
import org.slf4j.LoggerFactory;
@@ -52,31 +51,19 @@ public AllOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath
5251
}
5352

5453
@Override
55-
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
56-
return validate(executionContext, node, rootNode, instanceLocation, false);
54+
public void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
55+
validate(executionContext, node, rootNode, instanceLocation, false);
5756
}
5857

59-
protected Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean walk) {
58+
protected void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean walk) {
6059
debug(logger, executionContext, node, rootNode, instanceLocation);
6160

62-
SetView<ValidationMessage> childSchemaErrors = null;
63-
6461
for (JsonSchema schema : this.schemas) {
65-
Set<ValidationMessage> localErrors = null;
66-
6762
if (!walk) {
68-
localErrors = schema.validate(executionContext, node, rootNode, instanceLocation);
63+
schema.validate(executionContext, node, rootNode, instanceLocation);
6964
} else {
70-
localErrors = schema.walk(executionContext, node, rootNode, instanceLocation, true);
71-
}
72-
73-
if (localErrors != null && !localErrors.isEmpty()) {
74-
if (childSchemaErrors == null) {
75-
childSchemaErrors = new SetView<>();
76-
}
77-
childSchemaErrors.union(localErrors);
65+
schema.walk(executionContext, node, rootNode, instanceLocation, true);
7866
}
79-
8067
if (this.validationContext.getConfig().isDiscriminatorKeywordEnabled()) {
8168
final Iterator<JsonNode> arrayElements = this.schemaNode.elements();
8269
while (arrayElements.hasNext()) {
@@ -106,21 +93,19 @@ protected Set<ValidationMessage> validate(ExecutionContext executionContext, Jso
10693
}
10794
}
10895
}
109-
110-
return childSchemaErrors != null ? childSchemaErrors : Collections.emptySet();
11196
}
11297

11398
@Override
114-
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
99+
public void walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
115100
if (shouldValidateSchema && node != null) {
116-
return validate(executionContext, node, rootNode, instanceLocation, true);
101+
validate(executionContext, node, rootNode, instanceLocation, true);
102+
return;
117103
}
118104
for (JsonSchema schema : this.schemas) {
119105
// Walk through the schema
120106
schema.walk(executionContext, node, rootNode, instanceLocation, false);
121107
}
122-
return Collections.emptySet();
123-
}
108+
}
124109

125110
@Override
126111
public void preloadJsonSchema() {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
2020

21-
import java.util.Collections;
22-
import java.util.Set;
23-
2421
/**
2522
* Used for Keywords that have no validation aspect, but are part of the metaschema, where annotations may need to be collected.
2623
*/
@@ -33,15 +30,14 @@ public Validator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, Jso
3330
}
3431

3532
@Override
36-
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
33+
public void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation) {
3734
if (collectAnnotations(executionContext)) {
3835
Object value = getAnnotationValue(getSchemaNode());
3936
if (value != null) {
4037
putAnnotation(executionContext,
4138
annotation -> annotation.instanceLocation(instanceLocation).value(value));
4239
}
4340
}
44-
return Collections.emptySet();
4541
}
4642

4743
private Object getAnnotationValue(JsonNode schemaNode) {

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

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

1919
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.networknt.schema.utils.SetView;
2120

2221
import org.slf4j.Logger;
2322
import org.slf4j.LoggerFactory;
@@ -54,45 +53,48 @@ public AnyOfValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath
5453
}
5554

5655
@Override
57-
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
56+
public void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
5857
JsonNodePath instanceLocation) {
59-
return validate(executionContext, node, rootNode, instanceLocation, false);
58+
validate(executionContext, node, rootNode, instanceLocation, false);
6059
}
6160

62-
protected Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
61+
protected void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
6362
JsonNodePath instanceLocation, boolean walk) {
6463
debug(logger, executionContext, node, rootNode, instanceLocation);
6564

6665
if (this.validationContext.getConfig().isDiscriminatorKeywordEnabled()) {
6766
executionContext.enterDiscriminatorContext(new DiscriminatorContext(), instanceLocation);
6867
}
69-
SetView<ValidationMessage> allErrors = null;
70-
7168
int numberOfValidSubSchemas = 0;
69+
List<ValidationMessage> existingErrors = executionContext.getErrors();
70+
List<ValidationMessage> allErrors = null;
71+
List<ValidationMessage> errors = new ArrayList<>();
72+
executionContext.setErrors(errors);
7273
try {
7374
// Save flag as nested schema evaluation shouldn't trigger fail fast
7475
boolean failFast = executionContext.isFailFast();
7576
try {
7677
executionContext.setFailFast(false);
7778
for (JsonSchema schema : this.schemas) {
78-
Set<ValidationMessage> errors = Collections.emptySet();
79+
errors.clear();
7980
TypeValidator typeValidator = schema.getTypeValidator();
8081
if (typeValidator != null) {
8182
// If schema has type validator and node type doesn't match with schemaType then
8283
// ignore it
8384
// For union type, it is a must to call TypeValidator
8485
if (typeValidator.getSchemaType() != JsonType.UNION && !typeValidator.equalsToSchemaType(node)) {
86+
typeValidator.validate(executionContext, node, rootNode, instanceLocation);
8587
if (allErrors == null) {
86-
allErrors = new SetView<>();
88+
allErrors = new ArrayList<>();
8789
}
88-
allErrors.union(typeValidator.validate(executionContext, node, rootNode, instanceLocation));
90+
allErrors.addAll(errors);
8991
continue;
9092
}
9193
}
9294
if (!walk) {
93-
errors = schema.validate(executionContext, node, rootNode, instanceLocation);
95+
schema.validate(executionContext, node, rootNode, instanceLocation);
9496
} else {
95-
errors = schema.walk(executionContext, node, rootNode, instanceLocation, true);
97+
schema.walk(executionContext, node, rootNode, instanceLocation, true);
9698
}
9799

98100
// check if any validation errors have occurred
@@ -105,8 +107,8 @@ protected Set<ValidationMessage> validate(ExecutionContext executionContext, Jso
105107
&& canShortCircuit() && canShortCircuit(executionContext)) {
106108
// Clear all errors. Note that this is checked in finally.
107109
allErrors = null;
108-
// return empty errors.
109-
return errors;
110+
executionContext.setErrors(existingErrors);
111+
return;
110112
} else if (this.validationContext.getConfig().isDiscriminatorKeywordEnabled()) {
111113
DiscriminatorContext currentDiscriminatorContext = executionContext.getCurrentDiscriminatorContext();
112114
if (currentDiscriminatorContext.isDiscriminatorMatchFound()
@@ -116,24 +118,25 @@ && canShortCircuit() && canShortCircuit(executionContext)) {
116118
// which is generally discarded as it returns errors but the allErrors
117119
// is getting processed in finally
118120
if (allErrors == null) {
119-
allErrors = new SetView<>();
121+
allErrors = new ArrayList<>();
120122
}
121-
allErrors.union(Collections
122-
.singleton(message().instanceNode(node).instanceLocation(instanceLocation)
123-
.locale(executionContext.getExecutionConfig().getLocale())
124-
.failFast(executionContext.isFailFast()).arguments(DISCRIMINATOR_REMARK)
125-
.build()));
123+
allErrors.add(message().instanceNode(node).instanceLocation(instanceLocation)
124+
.locale(executionContext.getExecutionConfig().getLocale())
125+
.failFast(executionContext.isFailFast()).arguments(DISCRIMINATOR_REMARK)
126+
.build());
126127
} else {
127128
// Clear all errors. Note that this is checked in finally.
128129
allErrors = null;
129130
}
130-
return errors;
131+
existingErrors.addAll(errors);
132+
executionContext.setErrors(existingErrors);
133+
return;
131134
}
132135
}
133136
if (allErrors == null) {
134-
allErrors = new SetView<>();
137+
allErrors = new ArrayList<>();
135138
}
136-
allErrors.union(errors);
139+
allErrors.addAll(errors);
137140
}
138141
} finally {
139142
// Restore flag
@@ -143,32 +146,38 @@ && canShortCircuit() && canShortCircuit(executionContext)) {
143146
if (this.validationContext.getConfig().isDiscriminatorKeywordEnabled()
144147
&& executionContext.getCurrentDiscriminatorContext().isActive()
145148
&& !executionContext.getCurrentDiscriminatorContext().isDiscriminatorIgnore()) {
146-
return Collections.singleton(message().instanceNode(node).instanceLocation(instanceLocation)
149+
existingErrors.add(message().instanceNode(node).instanceLocation(instanceLocation)
147150
.locale(executionContext.getExecutionConfig().getLocale())
148151
.arguments(
149152
"based on the provided discriminator. No alternative could be chosen based on the discriminator property")
150153
.build());
154+
executionContext.setErrors(existingErrors);
155+
return;
151156
}
152157
} finally {
153158
if (this.validationContext.getConfig().isDiscriminatorKeywordEnabled()) {
154159
executionContext.leaveDiscriminatorContextImmediately(instanceLocation);
155160
}
156161
}
157162
if (numberOfValidSubSchemas >= 1) {
158-
return Collections.emptySet();
163+
executionContext.setErrors(existingErrors);
164+
} else {
165+
if (allErrors != null) {
166+
existingErrors.addAll(allErrors);
167+
}
168+
executionContext.setErrors(existingErrors);
159169
}
160-
return allErrors != null ? allErrors : Collections.emptySet();
161170
}
162171

163172
@Override
164-
public Set<ValidationMessage> walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
173+
public void walk(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, JsonNodePath instanceLocation, boolean shouldValidateSchema) {
165174
if (shouldValidateSchema && node != null) {
166-
return validate(executionContext, node, rootNode, instanceLocation, true);
175+
validate(executionContext, node, rootNode, instanceLocation, true);
176+
return;
167177
}
168178
for (JsonSchema schema : this.schemas) {
169179
schema.walk(executionContext, node, rootNode, instanceLocation, false);
170180
}
171-
return Collections.emptySet();
172181
}
173182

174183
/**

0 commit comments

Comments
 (0)