Skip to content

Commit 6717cea

Browse files
committed
fix for performance issue
1 parent c0e2c24 commit 6717cea

File tree

5 files changed

+1943
-39
lines changed

5 files changed

+1943
-39
lines changed

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,28 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
4343
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
4444
debug(logger, node, rootNode, at);
4545

46+
Set<ValidationMessage> allErrors = new LinkedHashSet<ValidationMessage>();
4647
String typeValidatorName = "anyOf/type";
47-
JsonType nodeType = TypeFactory.getValueNodeType(node);
48-
//If schema has type validator and it doesn't match with node type then ignore it
49-
List<JsonSchema> filteredSchemaList = new ArrayList<JsonSchema>();
5048
List<String> expectedTypeList = new ArrayList<String>();
49+
5150
for (JsonSchema schema : schemas) {
5251
if (schema.validators.containsKey(typeValidatorName)) {
53-
JsonType schemaType = ((TypeValidator) schema.validators.get(typeValidatorName)).getSchemaType();
54-
if (schemaType == nodeType) {
55-
filteredSchemaList.add(schema);
52+
TypeValidator typeValidator = ((TypeValidator) schema.validators.get(typeValidatorName));
53+
//If schema has type validator and node type doesn't match with schemaType then ignore it
54+
if (!typeValidator.equalsToSchemaType(node)) {
55+
expectedTypeList.add(typeValidator.getSchemaType().toString());
56+
continue;
5657
}
57-
expectedTypeList.add(schemaType.toString());
58-
}
59-
else {
60-
filteredSchemaList.add(schema);
6158
}
62-
}
63-
if (!schemas.isEmpty() && filteredSchemaList.isEmpty()) {
64-
return Collections.singleton(buildValidationMessage(at, StringUtils.join(expectedTypeList)));
65-
}
66-
67-
Set<ValidationMessage> allErrors = new LinkedHashSet<ValidationMessage>();
68-
69-
for (JsonSchema schema : filteredSchemaList) {
7059
Set<ValidationMessage> errors = schema.validate(node, rootNode, at);
7160
if (errors.isEmpty()) {
7261
return errors;
7362
}
7463
allErrors.addAll(errors);
7564
}
76-
65+
if (!schemas.isEmpty()) {
66+
return Collections.singleton(buildValidationMessage(at, StringUtils.join(expectedTypeList)));
67+
}
7768
return Collections.unmodifiableSet(allErrors);
7869
}
7970

0 commit comments

Comments
 (0)