Skip to content

Commit bb63267

Browse files
authored
Use full schema path to look up type validators for anyOf operator (#663)
* Add failing test for misleading validation of optional objects * Look up `anyOf` type validators by the full schema path
1 parent b888a00 commit bb63267

File tree

8 files changed

+283
-180
lines changed

8 files changed

+283
-180
lines changed

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

Lines changed: 177 additions & 173 deletions
Large diffs are not rendered by default.

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
*/
3131
public class BaseJsonSchemaValidatorTest {
3232

33-
private ObjectMapper mapper = new ObjectMapper();
33+
private static final ObjectMapper mapper = new ObjectMapper();
3434

3535
protected JsonNode getJsonNodeFromClasspath(String name) throws IOException {
3636
InputStream is1 = Thread.currentThread().getContextClassLoader()
37-
.getResourceAsStream(name);
37+
.getResourceAsStream(name);
3838
return mapper.readTree(is1);
3939
}
4040

@@ -46,10 +46,14 @@ protected JsonNode getJsonNodeFromUrl(String url) throws IOException {
4646
return mapper.readTree(new URL(url));
4747
}
4848

49-
protected JsonSchema getJsonSchemaFromClasspath(String name) {
50-
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
49+
protected static JsonSchema getJsonSchemaFromClasspath(String name) {
50+
return getJsonSchemaFromClasspath(name, SpecVersion.VersionFlag.V4);
51+
}
52+
53+
protected static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion) {
54+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(schemaVersion);
5155
InputStream is = Thread.currentThread().getContextClassLoader()
52-
.getResourceAsStream(name);
56+
.getResourceAsStream(name);
5357
return factory.getSchema(is);
5458
}
5559

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

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.IOException;
8+
import java.util.List;
9+
import java.util.Set;
10+
11+
import static java.util.stream.Collectors.toList;
12+
import static org.junit.jupiter.api.Assertions.assertFalse;
13+
import static org.junit.jupiter.api.Assertions.assertTrue;
14+
15+
public class Issue662Test extends BaseJsonSchemaValidatorTest {
16+
17+
private static final String RESOURCE_PREFIX = "issues/662/";
18+
private static JsonSchema schema;
19+
20+
@BeforeAll
21+
static void setup() {
22+
schema = getJsonSchemaFromClasspath(resource("schema.json"), SpecVersion.VersionFlag.V7);
23+
}
24+
25+
@Test
26+
void testNoErrorsForEmptyObject() throws IOException {
27+
JsonNode node = getJsonNodeFromClasspath(resource("emptyObject.json"));
28+
Set<ValidationMessage> errors = schema.validate(node);
29+
assertTrue(errors.isEmpty(), "No validation errors for empty optional object");
30+
}
31+
32+
@Test
33+
void testNoErrorsForValidObject() throws IOException {
34+
JsonNode node = getJsonNodeFromClasspath(resource("validObject.json"));
35+
Set<ValidationMessage> errors = schema.validate(node);
36+
assertTrue(errors.isEmpty(), "No validation errors for a valid optional object");
37+
}
38+
39+
@Test
40+
void testCorrectErrorForInvalidValue() throws IOException {
41+
JsonNode node = getJsonNodeFromClasspath(resource("objectInvalidValue.json"));
42+
Set<ValidationMessage> errors = schema.validate(node);
43+
List<String> errorMessages = errors.stream()
44+
.map(ValidationMessage::getMessage)
45+
.collect(toList());
46+
47+
assertTrue(
48+
errorMessages.contains("$.optionalObject.value: does not have a value in the enumeration [one, two]"),
49+
"Validation error for invalid object property is captured"
50+
);
51+
assertFalse(
52+
errorMessages.contains("$.optionalObject: object found, null expected"),
53+
"No validation error that the object is not expected"
54+
);
55+
}
56+
57+
private static String resource(String name) {
58+
return RESOURCE_PREFIX + name;
59+
}
60+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"optionalObject": {
3+
"value": "invalid"
4+
}
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"properties": {
4+
"optionalObject": {
5+
"anyOf": [
6+
{
7+
"type": "null"
8+
},
9+
{
10+
"type": "object",
11+
"properties": {
12+
"value": {
13+
"enum": [
14+
"one",
15+
"two"
16+
]
17+
}
18+
},
19+
"required": [
20+
"value"
21+
]
22+
}
23+
]
24+
}
25+
}
26+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"optionalObject": {
3+
"value": "one"
4+
}
5+
}

0 commit comments

Comments
 (0)