Skip to content

Commit b322058

Browse files
author
Mike Weber
authored
Fixed parallel processing (issue-335) (#377)
1 parent 9df611d commit b322058

16 files changed

+27
-47
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public AdditionalPropertiesValidator(String schemaPath, JsonNode schemaNode, Jso
4040
additionalPropertiesSchema = null;
4141
} else if (schemaNode.isObject()) {
4242
allowAdditionalProperties = true;
43-
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema)
44-
.initialize();
43+
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
4544
} else {
4645
allowAdditionalProperties = false;
4746
additionalPropertiesSchema = null;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
3131
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
3232
int size = schemaNode.size();
3333
for (int i = 0; i < size; i++) {
34-
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema)
35-
.initialize());
34+
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema));
3635
}
3736
}
3837

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
3131
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF, validationContext);
3232
int size = schemaNode.size();
3333
for (int i = 0; i < size; i++) {
34-
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema)
35-
.initialize());
34+
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema));
3635
}
3736
}
3837

@@ -43,8 +42,8 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
4342
String typeValidatorName = "anyOf/type";
4443

4544
for (JsonSchema schema : schemas) {
46-
if (schema.validators.containsKey(typeValidatorName)) {
47-
TypeValidator typeValidator = ((TypeValidator) schema.validators.get(typeValidatorName));
45+
if (schema.getValidators().containsKey(typeValidatorName)) {
46+
TypeValidator typeValidator = ((TypeValidator) schema.getValidators().get(typeValidatorName));
4847
//If schema has type validator and node type doesn't match with schemaType then ignore it
4948
//For union type, it is must to call TypeValidator
5049
if (typeValidator.getSchemaType() != JsonType.UNION && !typeValidator.equalsToSchemaType(node)) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public class ContainsValidator extends BaseJsonValidator implements JsonValidato
3131
public ContainsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3232
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.CONTAINS, validationContext);
3333
if (schemaNode.isObject() || schemaNode.isBoolean()) {
34-
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema)
35-
.initialize();
34+
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
3635
}
3736

3837
parseErrorCode(getValidatorType().getErrorCodeKey());

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public DependenciesValidator(String schemaPath, JsonNode schemaNode, JsonSchema
4444
depsProps.add(pvalue.get(i).asText());
4545
}
4646
} else if (pvalue.isObject() || pvalue.isBoolean()) {
47-
schemaDeps.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUri(), pvalue, parentSchema)
48-
.initialize());
47+
schemaDeps.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUri(), pvalue, parentSchema));
4948
}
5049
}
5150

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@ public IfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSche
3737
for (final String keyword : KEYWORDS) {
3838
final JsonNode node = schemaNode.get(keyword);
3939
if (keyword.equals("if")) {
40-
ifSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema)
41-
.initialize();
40+
ifSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
4241
} else if (keyword.equals("then") && node != null) {
43-
thenSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema)
44-
.initialize();
42+
thenSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
4543
} else if (keyword.equals("else") && node != null) {
46-
elseSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema)
47-
.initialize();
44+
elseSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
4845
}
4946
}
5047
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,20 @@ public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
4141
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ITEMS, validationContext);
4242
if (schemaNode.isObject() || schemaNode.isBoolean()) {
4343
schema = new JsonSchema(validationContext, schemaPath, parentSchema.getCurrentUri(), schemaNode,
44-
parentSchema).initialize();
44+
parentSchema);
4545
} else {
4646
tupleSchema = new ArrayList<JsonSchema>();
4747
for (JsonNode s : schemaNode) {
4848
tupleSchema.add(
49-
new JsonSchema(validationContext, schemaPath, parentSchema.getCurrentUri(), s, parentSchema)
50-
.initialize());
49+
new JsonSchema(validationContext, schemaPath, parentSchema.getCurrentUri(), s, parentSchema));
5150
}
5251

5352
JsonNode addItemNode = getParentSchema().getSchemaNode().get(PROPERTY_ADDITIONAL_ITEMS);
5453
if (addItemNode != null) {
5554
if (addItemNode.isBoolean()) {
5655
additionalItems = addItemNode.asBoolean();
5756
} else if (addItemNode.isObject()) {
58-
additionalSchema = new JsonSchema(validationContext, parentSchema.getCurrentUri(), addItemNode)
59-
.initialize();
57+
additionalSchema = new JsonSchema(validationContext, parentSchema.getCurrentUri(), addItemNode);
6058
}
6159
}
6260
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*/
3737
public class JsonSchema extends BaseJsonValidator {
3838
private static final Pattern intPattern = Pattern.compile("^[0-9]+$");
39-
protected Map<String, JsonValidator> validators;
39+
private Map<String, JsonValidator> validators;
4040
private final String idKeyword;
4141
private final ValidationContext validationContext;
4242
private WalkListenerRunner keywordWalkListenerRunner;
@@ -80,11 +80,6 @@ private JsonSchema(ValidationContext validationContext, String schemaPath, URI c
8080
}
8181
}
8282

83-
JsonSchema initialize() {
84-
this.validators = Collections.unmodifiableMap(this.read(getSchemaNode()));
85-
return this;
86-
}
87-
8883
private URI combineCurrentUriWithIds(URI currentUri, JsonNode schemaNode) {
8984
final String id = validationContext.resolveSchemaId(schemaNode);
9085
if (id == null) {
@@ -225,7 +220,7 @@ private Map<String, JsonValidator> read(JsonNode schemaNode) {
225220

226221
public Set<ValidationMessage> validate(JsonNode jsonNode, JsonNode rootNode, String at) {
227222
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
228-
for (JsonValidator v : validators.values()) {
223+
for (JsonValidator v : getValidators().values()) {
229224
errors.addAll(v.validate(jsonNode, rootNode, at));
230225
}
231226
return errors;
@@ -296,7 +291,7 @@ public ValidationResult walk(JsonNode node, boolean shouldValidateSchema) {
296291
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
297292
Set<ValidationMessage> validationMessages = new LinkedHashSet<ValidationMessage>();
298293
// Walk through all the JSONWalker's.
299-
for (Entry<String, JsonValidator> entry : validators.entrySet()) {
294+
for (Entry<String, JsonValidator> entry : getValidators().entrySet()) {
300295
JsonSchemaWalker jsonWalker = entry.getValue();
301296
String schemaPathWithKeyword = entry.getKey();
302297
try {
@@ -343,6 +338,9 @@ public JsonValidator getRequiredValidator() {
343338
}
344339

345340
public Map<String, JsonValidator> getValidators() {
341+
if (validators == null) {
342+
validators = Collections.unmodifiableMap(this.read(getSchemaNode()));
343+
}
346344
return validators;
347345
}
348346

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public static Builder builder(final JsonSchemaFactory blueprint) {
263263
protected JsonSchema newJsonSchema(final URI schemaUri, final JsonNode schemaNode, final SchemaValidatorsConfig config) {
264264
final ValidationContext validationContext = createValidationContext(schemaNode);
265265
validationContext.setConfig(config);
266-
return new JsonSchema(validationContext, schemaUri, schemaNode).initialize();
266+
return new JsonSchema(validationContext, schemaUri, schemaNode);
267267
}
268268

269269
protected ValidationContext createValidationContext(final JsonNode schemaNode) {
@@ -349,7 +349,6 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co
349349
jsonSchema = new JsonSchema(validationContext, mappedUri, schemaNode);
350350
}
351351
uriSchemaCache.put(mappedUri, jsonSchema);
352-
jsonSchema.initialize();
353352

354353
return jsonSchema;
355354
} finally {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public class NotValidator extends BaseJsonValidator implements JsonValidator {
3131

3232
public NotValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3333
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.NOT, validationContext);
34-
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema)
35-
.initialize();
34+
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
3635

3736
parseErrorCode(getValidatorType().getErrorCodeKey());
3837
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ public OneOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
118118
int size = schemaNode.size();
119119
for (int i = 0; i < size; i++) {
120120
JsonNode childNode = schemaNode.get(i);
121-
JsonSchema childSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), childNode, parentSchema)
122-
.initialize();
121+
JsonSchema childSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), childNode, parentSchema);
123122
schemas.add(new ShortcutValidator(childNode, parentSchema, validationContext, childSchema));
124123
}
125124

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public PatternPropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSc
3838
Iterator<String> names = schemaNode.fieldNames();
3939
while (names.hasNext()) {
4040
String name = names.next();
41-
schemas.put(Pattern.compile(name), new JsonSchema(validationContext, name, parentSchema.getCurrentUri(), schemaNode.get(name), parentSchema)
42-
.initialize());
41+
schemas.put(Pattern.compile(name), new JsonSchema(validationContext, name, parentSchema.getCurrentUri(), schemaNode.get(name), parentSchema));
4342
}
4443
}
4544

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public PropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSchema pa
3737
schemas = new HashMap<String, JsonSchema>();
3838
for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) {
3939
String pname = it.next();
40-
schemas.put(pname, new JsonSchema(validationContext, schemaPath + "/" + pname, parentSchema.getCurrentUri(), schemaNode.get(pname), parentSchema)
41-
.initialize());
40+
schemas.put(pname, new JsonSchema(validationContext, schemaPath + "/" + pname, parentSchema.getCurrentUri(), schemaNode.get(pname), parentSchema));
4241
}
4342
propertyWalkListenerRunner = new DefaultPropertyWalkListenerRunner(
4443
config.getPropertyWalkListeners());

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public PropertyNamesValidator(String schemaPath, JsonNode schemaNode, JsonSchema
3434
schemas = new HashMap<String, JsonSchema>();
3535
for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) {
3636
String pname = it.next();
37-
schemas.put(pname, new JsonSchema(validationContext, schemaPath + "/" + pname, parentSchema.getCurrentUri(), schemaNode.get(pname), parentSchema)
38-
.initialize());
37+
schemas.put(pname, new JsonSchema(validationContext, schemaPath + "/" + pname, parentSchema.getCurrentUri(), schemaNode.get(pname), parentSchema));
3938
}
4039
}
4140
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ static JsonSchemaRef getRefSchema(JsonSchema parentSchema, ValidationContext val
9797
if (ref == null) {
9898
ref = new JsonSchemaRef(validationContext, refValue);
9999
validationContext.setReferenceParsingInProgress(refValueOriginal, ref);
100-
JsonSchema ret = new JsonSchema(validationContext, refValue, parentSchema.getCurrentUri(), node, parentSchema)
101-
.initialize();
100+
JsonSchema ret = new JsonSchema(validationContext, refValue, parentSchema.getCurrentUri(), node, parentSchema);
102101
ref.set(ret);
103102
}
104103
return ref;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public UnionTypeValidator(String schemaPath, JsonNode schemaNode, JsonSchema par
5050
sep = ", ";
5151

5252
if (n.isObject())
53-
schemas.add(new JsonSchema(validationContext, ValidatorTypeCode.TYPE.getValue(), parentSchema.getCurrentUri(), n, parentSchema)
54-
.initialize());
53+
schemas.add(new JsonSchema(validationContext, ValidatorTypeCode.TYPE.getValue(), parentSchema.getCurrentUri(), n, parentSchema));
5554
else
5655
schemas.add(new TypeValidator(schemaPath + "/" + i, n, parentSchema, validationContext));
5756

0 commit comments

Comments
 (0)