Skip to content

Commit e68be00

Browse files
authored
Revert "The bug fix for the issue 520 (#543)" (#547)
This reverts commit ae7c0ca.
1 parent ae7c0ca commit e68be00

18 files changed

+90
-248
lines changed

pom.xml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
<version.hamcrest>2.2</version.hamcrest>
7373
<version.undertow>2.2.14.Final</version.undertow>
7474
<version.itu>1.5.1</version.itu>
75-
<version.commons.io>2.11.0</version.commons.io>
7675
</properties>
7776
<dependencies>
7877
<dependency>
@@ -119,12 +118,6 @@
119118
<version>${version.junit}</version>
120119
<scope>test</scope>
121120
</dependency>
122-
<dependency>
123-
<groupId>junit</groupId>
124-
<artifactId>junit</artifactId>
125-
<version>4.13.2</version>
126-
<scope>test</scope>
127-
</dependency>
128121
<dependency>
129122
<groupId>org.mockito</groupId>
130123
<artifactId>mockito-core</artifactId>
@@ -143,12 +136,6 @@
143136
<version>${version.undertow}</version>
144137
<scope>test</scope>
145138
</dependency>
146-
<dependency>
147-
<groupId>commons-io</groupId>
148-
<artifactId>commons-io</artifactId>
149-
<version>${version.commons.io}</version>
150-
<scope>test</scope>
151-
</dependency>
152139
</dependencies>
153140
<build>
154141
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.LinkedHashSet;
2323
import java.util.List;
2424
import java.util.Set;
25-
import java.util.stream.Collectors;
2625

2726
import com.fasterxml.jackson.databind.JsonNode;
2827
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -50,7 +49,7 @@ public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
5049
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
5150
debug(logger, node, rootNode, at);
5251

53-
Set<ValidationMessage> childSchemaErrors = new LinkedHashSet<>();
52+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
5453

5554
// As AllOf might contain multiple schemas take a backup of evaluatedProperties.
5655
Object backupEvaluatedProperties = CollectorContext.getInstance().get(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES);
@@ -60,7 +59,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6059

6160
for (JsonSchema schema : schemas) {
6261
try {
63-
childSchemaErrors.addAll(schema.validate(node, rootNode, at));
62+
errors.addAll(schema.validate(node, rootNode, at));
6463

6564
if (this.validationContext.getConfig().isOpenAPI3StyleDiscriminators()) {
6665
final Iterator<JsonNode> arrayElements = schemaNode.elements();
@@ -94,7 +93,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
9493
}
9594
}
9695
} finally {
97-
if (childSchemaErrors.isEmpty()) {
96+
if (errors.isEmpty()) {
9897
List<String> backupEvaluatedPropertiesList = (backupEvaluatedProperties == null ? new ArrayList<>() : (List<String>) backupEvaluatedProperties);
9998
backupEvaluatedPropertiesList.addAll((List<String>) CollectorContext.getInstance().get(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES));
10099
CollectorContext.getInstance().add(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES, backupEvaluatedPropertiesList);
@@ -104,13 +103,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
104103
}
105104
}
106105

107-
if(!childSchemaErrors.isEmpty()){
108-
List<String> childSchemaErrorMessages = childSchemaErrors.stream().map(ValidationMessage::getMessage).collect(Collectors.toList());
109-
String childMessages = String.join(", ", childSchemaErrorMessages);
110-
return Collections.singleton(ValidationMessage.of(getValidatorType().getValue(), ValidatorTypeCode.ALL_OF, at, childMessages));
111-
}
112-
113-
return Collections.unmodifiableSet(childSchemaErrors);
106+
return Collections.unmodifiableSet(errors);
114107
}
115108

116109
@Override

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
113113
CollectorContext.getInstance().add(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES, backupEvaluatedProperties);
114114
}
115115
}
116-
if (!allErrors.isEmpty()) {
117-
List<String> childSchemaErrorMessages = allErrors.stream().map(ValidationMessage::getMessage).collect(Collectors.toList());
118-
String childMessages = String.join(", ", childSchemaErrorMessages);
119-
return Collections.singleton(ValidationMessage.of(getValidatorType().getValue(), ValidatorTypeCode.ANY_OF, at, childMessages));
120-
}
121116
return Collections.unmodifiableSet(allErrors);
122117
}
123118

@@ -134,12 +129,12 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
134129
for (JsonSchema schema : schemas) {
135130
results.add(schema.walk(node, rootNode, at, shouldValidateSchema));
136131
}
137-
if (!shouldValidateSchema) {
132+
if(! shouldValidateSchema) {
138133
return new LinkedHashSet<>();
139134
}
140135
boolean atLeastOneValid = results.stream()
141136
.anyMatch(Set::isEmpty);
142-
if (atLeastOneValid) {
137+
if(atLeastOneValid) {
143138
return new LinkedHashSet<>();
144139
}
145140
return results.stream()

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

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public OneOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
126126
}
127127

128128
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
129-
Set<ValidationMessage> errors = new LinkedHashSet<>();
129+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
130130

131131
// As oneOf might contain multiple schemas take a backup of evaluatedProperties.
132132
Object backupEvaluatedProperties = CollectorContext.getInstance().get(UnEvaluatedPropertiesValidator.EVALUATED_PROPERTIES);
@@ -143,13 +143,14 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
143143
state.setComplexValidator(true);
144144

145145
int numberOfValidSchema = 0;
146-
Set<ValidationMessage> childErrors = new LinkedHashSet<>();
146+
Set<ValidationMessage> childErrors = new LinkedHashSet<ValidationMessage>();
147147
// validate that only a single element has been received in the oneOf node
148148
// validation should not continue, as it contradicts the oneOf requirement of only one
149149
// if(node.isObject() && node.size()>1) {
150150
// errors = Collections.singleton(buildValidationMessage(at, ""));
151151
// return Collections.unmodifiableSet(errors);
152152
// }
153+
153154
for (ShortcutValidator validator : schemas) {
154155
Set<ValidationMessage> schemaErrors = null;
155156
// Reset state in case the previous validator did not match
@@ -190,15 +191,36 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
190191
Set<ValidationMessage> childNotRequiredErrors = childErrors.stream().filter(error -> !ValidatorTypeCode.REQUIRED.getValue().equals(error.getType())).collect(Collectors.toSet());
191192

192193
// ensure there is always an "OneOf" error reported if number of valid schemas is not equal to 1.
193-
// TODO We need to break it into two errors in the future.
194-
if (numberOfValidSchema != 1) {
194+
if (numberOfValidSchema > 1) {
195195
final ValidationMessage message = getMultiSchemasValidErrorMsg(at);
196196
if (failFast) {
197197
throw new JsonSchemaException(message);
198198
}
199199
errors.add(message);
200200
}
201201

202+
// ensure there is always an "OneOf" error reported if number of valid schemas is not equal to 1.
203+
else if (numberOfValidSchema < 1) {
204+
if (!childNotRequiredErrors.isEmpty()) {
205+
childErrors = childNotRequiredErrors;
206+
}
207+
if (!childErrors.isEmpty()) {
208+
if (childErrors.size() > 1) {
209+
Set<ValidationMessage> notAdditionalPropertiesOnly = new LinkedHashSet<>(childErrors.stream()
210+
.filter((ValidationMessage validationMessage) -> !ValidatorTypeCode.ADDITIONAL_PROPERTIES.getValue().equals(validationMessage.getType()))
211+
.sorted((vm1, vm2) -> compareValidationMessages(vm1, vm2))
212+
.collect(Collectors.toList()));
213+
if (notAdditionalPropertiesOnly.size() > 0) {
214+
childErrors = notAdditionalPropertiesOnly;
215+
}
216+
}
217+
errors.addAll(childErrors);
218+
}
219+
if (failFast) {
220+
throw new JsonSchemaException(errors.toString());
221+
}
222+
}
223+
202224
// Make sure to signal parent handlers we matched
203225
if (errors.isEmpty())
204226
state.setMatchedNode(true);
@@ -218,34 +240,84 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
218240
}
219241
}
220242

243+
/**
244+
* Sort <code>ValidationMessage</code> by its type
245+
* @return
246+
*/
247+
private static int compareValidationMessages(ValidationMessage vm1, ValidationMessage vm2) {
248+
// ValidationMessage's type has smaller index in the list below has high priority
249+
final List<String> typeCodes = Arrays.asList(
250+
ValidatorTypeCode.TYPE.getValue(),
251+
ValidatorTypeCode.DATETIME.getValue(),
252+
ValidatorTypeCode.UUID.getValue(),
253+
ValidatorTypeCode.ID.getValue(),
254+
ValidatorTypeCode.EXCLUSIVE_MAXIMUM.getValue(),
255+
ValidatorTypeCode.EXCLUSIVE_MINIMUM.getValue(),
256+
ValidatorTypeCode.TRUE.getValue(),
257+
ValidatorTypeCode.FALSE.getValue(),
258+
ValidatorTypeCode.CONST.getValue(),
259+
ValidatorTypeCode.CONTAINS.getValue(),
260+
ValidatorTypeCode.PROPERTYNAMES.getValue()
261+
);
262+
263+
final int index1 = typeCodes.indexOf(vm1.getType());
264+
final int index2 = typeCodes.indexOf(vm2.getType());
265+
266+
if (index1 >= 0) {
267+
if (index2 >= 0) {
268+
return Integer.compare(index1, index2);
269+
} else {
270+
return -1;
271+
}
272+
} else {
273+
if (index2 >= 0) {
274+
return 1;
275+
} else {
276+
return vm1.getCode().compareTo(vm2.getCode());
277+
}
278+
}
279+
}
280+
221281
private void resetValidatorState() {
222282
ValidatorState state = (ValidatorState) CollectorContext.getInstance().get(ValidatorState.VALIDATOR_STATE_KEY);
223283
state.setComplexValidator(false);
224284
state.setMatchedNode(true);
225285
}
226286

287+
public List<JsonSchema> getChildSchemas() {
288+
List<JsonSchema> childJsonSchemas = new ArrayList<JsonSchema>();
289+
for (ShortcutValidator shortcutValidator: schemas ) {
290+
childJsonSchemas.add(shortcutValidator.getSchema());
291+
}
292+
return childJsonSchemas;
293+
}
294+
227295
@Override
228296
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
229297
HashSet<ValidationMessage> validationMessages = new LinkedHashSet<ValidationMessage>();
230298
if (shouldValidateSchema) {
231299
validationMessages.addAll(validate(node, rootNode, at));
232300
} else {
233301
for (ShortcutValidator validator : schemas) {
234-
validator.schema.walk(node, rootNode, at, shouldValidateSchema);
302+
validator.schema.walk(node, rootNode, at , shouldValidateSchema);
235303
}
236304
}
237305
return validationMessages;
238306
}
239307

240-
private ValidationMessage getMultiSchemasValidErrorMsg(String at) {
241-
List<String> msgStrList = schemas.stream().map(shortcutValidator -> shortcutValidator.getSchema().getSchemaNode().toString()).collect(Collectors.toList());
242-
String msg = String.join(", ", msgStrList);
308+
private ValidationMessage getMultiSchemasValidErrorMsg(String at){
309+
String msg="";
310+
for(ShortcutValidator schema: schemas){
311+
String schemaValue = schema.getSchema().getSchemaNode().toString();
312+
msg = msg.concat(schemaValue);
313+
}
314+
243315
return ValidationMessage.of(getValidatorType().getValue(), ValidatorTypeCode.ONE_OF, at, msg);
244316
}
245317

246318
@Override
247319
public void preloadJsonSchema() {
248-
for (final ShortcutValidator scValidator : schemas) {
320+
for (final ShortcutValidator scValidator: schemas) {
249321
scValidator.getSchema().initializeValidators();
250322
}
251323
}

src/main/resources/jsv-messages.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$ref = {0}: has an error with 'refs'
22
additionalProperties = {0}.{1}: is not defined in the schema and the schema does not allow additional properties
3-
allOf = {0}: should be valid to all the schemas: {1}
4-
anyOf = {0}: should be valid to one schema at least: {1}
3+
allOf = {0}: should be valid to all the schemas {1}
4+
anyOf = {0}: should be valid to any of the schemas {1}
55
const = {0}: must be a constant value {1}
66
contains = {0}: does not contain an element that passes these validations: {1}
77
crossEdits = {0}: has an error with 'cross edits'
@@ -28,7 +28,7 @@ minimum = {0}: must have a minimum value of {1}
2828
multipleOf = {0}: must be multiple of {1}
2929
not = {0}: should not be valid to the schema {1}
3030
notAllowed = {0}.{1}: is not allowed but it is in the data
31-
oneOf = {0}: should be valid to one and only one of schema, but more than one are valid or no one valid: {1}
31+
oneOf = {0}: should be valid to one and only one of schema, but more than one are valid: {1}
3232
pattern = {0}: does not match the regex pattern {1}
3333
patternProperties = {0}: has some error with 'pattern properties'
3434
properties = {0}: has an error with 'properties'

src/test/java/com/networknt/schema/AllOfValidatorTest.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/test/java/com/networknt/schema/AnyOfValidatorTest.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/test/java/com/networknt/schema/OneOfValidatorTest.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/test/resources/data/issue520/issue520-allOfValidator-invalid.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)