Skip to content

Commit

Permalink
Potential fix for networknt#383
Browse files Browse the repository at this point in the history
- signal nodeMatch to parent handler in OneOfValidator
  • Loading branch information
JonasProgrammer committed Mar 13, 2021
1 parent e692d47 commit 09b2b04
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/networknt/schema/OneOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ else if (numberOfValidSchema < 1) {
}
}

// Make sure to signal parent handlers we matched
if (errors.isEmpty())
state.setMatchedNode(true);

// reset the ValidatorState object in the ThreadLocal
validatorState.remove();
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/networknt/schema/Issue383Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.networknt.schema;

import java.io.InputStream;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Issue383Test {
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
return factory.getSchema(schemaContent);
}

protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(content);
return node;
}

@Test
public void nestedOneOfsShouldStillMatchV7() throws Exception {
String schemaPath = "/schema/issue383-v7.json";
String dataPath = "/data/issue383.json";
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream);
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
Set<ValidationMessage> errors = schema.validate(node);
Assert.assertEquals(0, errors.size());
}
}
11 changes: 11 additions & 0 deletions src/test/resources/data/issue383.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"validation": {
"all": [
{
"notEmpty": {
"field": "test"
}
}
]
}
}
85 changes: 85 additions & 0 deletions src/test/resources/schema/issue383-v7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"type": "object",
"properties": {
"validation": {
"$ref": "#/definitions/predicateOrNull"
}
},
"definitions": {
"fieldRef": {
"type": "object",
"properties": {
"field": {
"type": "string"
},
"set": {
"type": "string"
}
},
"required": [
"field"
]
},
"patternPredicate": {
"type": "object",
"oneOf": [
{
"properties": {
"notEmpty": {
"$ref": "#/definitions/fieldRef"
}
},
"required": [
"notEmpty"
]
},
{
"properties": {
"notBlank": {
"$ref": "#/definitions/fieldRef"
}
},
"required": [
"notBlank"
]
}
]
},
"allPredicate": {
"type": "object",
"properties": {
"all": {
"type": "array",
"minItems": 1,
"items": {
"$ref": "#/definitions/predicate"
}
}
},
"required": [
"all"
]
},
"predicate": {
"type": "object",
"oneOf": [
{
"$ref": "#/definitions/patternPredicate"
},
{
"$ref": "#/definitions/allPredicate"
}
]
},
"predicateOrNull": {
"oneOf": [
{
"$ref": "#/definitions/predicate"
},
{
"type": "null"
}
]
}
}
}

0 comments on commit 09b2b04

Please sign in to comment.