forked from networknt/json-schema-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Issue428: Handle nullable for oneOf and the child node * Issue428: Fix the duplicate validation for the oneOf type
- Loading branch information
Showing
7 changed files
with
458 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ dist/ | |
.settings | ||
.metadata/ | ||
*.iml | ||
*.ipr | ||
*.iws | ||
*.log | ||
*.tmp | ||
*.zip | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/com/networknt/schema/utils/JsonNodeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.networknt.schema.utils; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.networknt.schema.CollectorContext; | ||
import com.networknt.schema.JsonType; | ||
import com.networknt.schema.SchemaValidatorsConfig; | ||
import com.networknt.schema.ValidatorState; | ||
|
||
import java.util.Iterator; | ||
|
||
public class JsonNodeUtil { | ||
|
||
public static boolean isNodeNullable(JsonNode schema){ | ||
JsonNode nullable = schema.get("nullable"); | ||
if (nullable != null && nullable.asBoolean()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
//Check to see if a JsonNode is nullable with checking the isHandleNullableField | ||
public static boolean isNodeNullable(JsonNode schema, SchemaValidatorsConfig config){ | ||
// check if the parent schema declares the fields as nullable | ||
if (config.isHandleNullableField()) { | ||
return isNodeNullable(schema); | ||
} | ||
return false; | ||
} | ||
|
||
//Check to see if any child node for the OneOf SchemaNode is nullable | ||
public static boolean isChildNodeNullable(ArrayNode oneOfSchemaNode,SchemaValidatorsConfig config){ | ||
Iterator iterator = oneOfSchemaNode.elements(); | ||
while(iterator.hasNext()){ | ||
//If one of the child Node for oneOf is nullable, it means the whole oneOf is nullable | ||
if (isNodeNullable((JsonNode)iterator.next(),config)) return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static boolean matchOneOfTypeNode(JsonNode oneOfSchemaNode, JsonType nodeType ){ | ||
Iterator iterator = oneOfSchemaNode.elements(); | ||
while (iterator.hasNext()){ | ||
JsonNode oneOfTypeNode = (JsonNode) iterator.next(); | ||
JsonNode typeTextNode = oneOfTypeNode.get("type"); | ||
if(typeTextNode != null && typeTextNode.asText().equals(nodeType.toString())) //If the nodeType is oneOf the type defined in the oneOf , return true | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.networknt.schema; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import io.undertow.Undertow; | ||
import io.undertow.server.handlers.resource.FileResourceManager; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static io.undertow.Handlers.resource; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
public class Issue428Test { | ||
protected ObjectMapper mapper = new ObjectMapper(); | ||
protected JsonSchemaFactory validatorFactory = JsonSchemaFactory | ||
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).objectMapper(mapper).build(); | ||
protected static Undertow server = null; | ||
|
||
public Issue428Test() { | ||
} | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
if (server == null) { | ||
server = Undertow.builder() | ||
.addHttpListener(1234, "localhost") | ||
.setHandler(resource(new FileResourceManager( | ||
new File("./src/test/resources/remotes"), 100))) | ||
.build(); | ||
server.start(); | ||
} | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() throws Exception { | ||
if (server != null) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException ignored) { | ||
|
||
} | ||
server.stop(); | ||
} | ||
} | ||
|
||
private void runTestFile(String testCaseFile) throws Exception { | ||
final URI testCaseFileUri = URI.create("classpath:" + testCaseFile); | ||
InputStream in = Thread.currentThread().getContextClassLoader() | ||
.getResourceAsStream(testCaseFile); | ||
ArrayNode testCases = mapper.readValue(in, ArrayNode.class); | ||
|
||
for (int j = 0; j < testCases.size(); j++) { | ||
try { | ||
JsonNode testCase = testCases.get(j); | ||
SchemaValidatorsConfig config = new SchemaValidatorsConfig(); | ||
|
||
ArrayNode testNodes = (ArrayNode) testCase.get("tests"); | ||
for (int i = 0; i < testNodes.size(); i++) { | ||
JsonNode test = testNodes.get(i); | ||
System.out.println("=== " + test.get("description")); | ||
JsonNode node = test.get("data"); | ||
JsonNode typeLooseNode = test.get("isTypeLoose"); | ||
// Configure the schemaValidator to set typeLoose's value based on the test file, | ||
// if test file do not contains typeLoose flag, use default value: true. | ||
config.setTypeLoose(typeLooseNode != null && typeLooseNode.asBoolean()); | ||
config.setOpenAPI3StyleDiscriminators(false); | ||
JsonSchema schema = validatorFactory.getSchema(testCaseFileUri, testCase.get("schema"), config); | ||
|
||
List<ValidationMessage> errors = new ArrayList<ValidationMessage>(schema.validate(node)); | ||
|
||
if (test.get("valid").asBoolean()) { | ||
if (!errors.isEmpty()) { | ||
System.out.println("---- test case failed ----"); | ||
System.out.println("schema: " + schema.toString()); | ||
System.out.println("data: " + test.get("data")); | ||
System.out.println("errors:"); | ||
for (ValidationMessage error : errors) { | ||
System.out.println(error); | ||
} | ||
} | ||
//assertEquals(2, errors.size()); | ||
} else { | ||
if (errors.isEmpty()) { | ||
System.out.println("---- test case failed ----"); | ||
System.out.println("schema: " + schema); | ||
System.out.println("data: " + test.get("data")); | ||
} else { | ||
JsonNode errorCount = test.get("errorCount"); | ||
if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) { | ||
System.out.println("---- test case failed ----"); | ||
System.out.println("schema: " + schema); | ||
System.out.println("data: " + test.get("data")); | ||
System.out.println("errors: " + errors); | ||
for (ValidationMessage error : errors) { | ||
System.out.println(error); | ||
} | ||
assertEquals("expected error count", errorCount.asInt(), errors.size()); | ||
} | ||
} | ||
assertFalse(errors.isEmpty()); | ||
} | ||
|
||
} | ||
|
||
|
||
} catch (JsonSchemaException e) { | ||
throw new IllegalStateException(String.format("Current schema should not be invalid: %s", testCaseFile), e); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testNullableOneOf() throws Exception { | ||
runTestFile("data/issue428.json"); | ||
} | ||
} |
Oops, something went wrong.