Skip to content

Commit 76ad178

Browse files
authored
Validation of oneOf depends on schema order (#361)
* Add unit test for Issue #295 * fixes #295 prevent state leakage between oneOf validation steps
1 parent cb332a3 commit 76ad178

4 files changed

Lines changed: 61 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
145145
// }
146146

147147
for (ShortcutValidator validator : schemas) {
148+
// Reset state in case the previous validator did not match
149+
state.setMatchedNode(true);
148150
if (!validator.allConstantsMatch(node)) {
149151
// take a shortcut: if there is any constant that does not match,
150152
// we can bail out of the validation
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.io.InputStream;
9+
import java.util.Set;
10+
11+
public class Issue295Test {
12+
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) {
13+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
14+
return factory.getSchema(schemaContent);
15+
}
16+
17+
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
18+
ObjectMapper mapper = new ObjectMapper();
19+
JsonNode node = mapper.readTree(content);
20+
return node;
21+
}
22+
23+
@Test
24+
public void shouldWorkV7() throws Exception {
25+
String schemaPath = "/schema/issue295-v7.json";
26+
String dataPath = "/data/issue295.json";
27+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
28+
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream);
29+
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
30+
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
31+
Set<ValidationMessage> errors = schema.validate(node);
32+
Assert.assertEquals(0, errors.size());
33+
}
34+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "other": "stuff" }
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"$id": "https://example.com/properties.schema.json",
3+
"$schema": "http://json-schema.org/draft-07/schema#",
4+
"oneOf": [
5+
{
6+
"required": [
7+
"requiredprop"
8+
],
9+
"properties": {
10+
"requiredprop": {
11+
"type": "string"
12+
}
13+
}
14+
},
15+
{
16+
"properties": {
17+
"forbiddenprop": {
18+
"type": "null"
19+
}
20+
}
21+
}
22+
]
23+
}
24+

0 commit comments

Comments
 (0)