Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
350 changes: 177 additions & 173 deletions src/main/java/com/networknt/schema/AnyOfValidator.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
*/
public class BaseJsonSchemaValidatorTest {

private ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper mapper = new ObjectMapper();

protected JsonNode getJsonNodeFromClasspath(String name) throws IOException {
InputStream is1 = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(name);
.getResourceAsStream(name);
return mapper.readTree(is1);
}

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

protected JsonSchema getJsonSchemaFromClasspath(String name) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
protected static JsonSchema getJsonSchemaFromClasspath(String name) {
return getJsonSchemaFromClasspath(name, SpecVersion.VersionFlag.V4);
}

protected static JsonSchema getJsonSchemaFromClasspath(String name, SpecVersion.VersionFlag schemaVersion) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(schemaVersion);
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(name);
.getResourceAsStream(name);
return factory.getSchema(is);
}

Expand Down
2 changes: 0 additions & 2 deletions src/test/java/com/networknt/schema/Issue627Test.java

This file was deleted.

60 changes: 60 additions & 0 deletions src/test/java/com/networknt/schema/Issue662Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class Issue662Test extends BaseJsonSchemaValidatorTest {

private static final String RESOURCE_PREFIX = "issues/662/";
private static JsonSchema schema;

@BeforeAll
static void setup() {
schema = getJsonSchemaFromClasspath(resource("schema.json"), SpecVersion.VersionFlag.V7);
}

@Test
void testNoErrorsForEmptyObject() throws IOException {
JsonNode node = getJsonNodeFromClasspath(resource("emptyObject.json"));
Set<ValidationMessage> errors = schema.validate(node);
assertTrue(errors.isEmpty(), "No validation errors for empty optional object");
}

@Test
void testNoErrorsForValidObject() throws IOException {
JsonNode node = getJsonNodeFromClasspath(resource("validObject.json"));
Set<ValidationMessage> errors = schema.validate(node);
assertTrue(errors.isEmpty(), "No validation errors for a valid optional object");
}

@Test
void testCorrectErrorForInvalidValue() throws IOException {
JsonNode node = getJsonNodeFromClasspath(resource("objectInvalidValue.json"));
Set<ValidationMessage> errors = schema.validate(node);
List<String> errorMessages = errors.stream()
.map(ValidationMessage::getMessage)
.collect(toList());

assertTrue(
errorMessages.contains("$.optionalObject.value: does not have a value in the enumeration [one, two]"),
"Validation error for invalid object property is captured"
);
assertFalse(
errorMessages.contains("$.optionalObject: object found, null expected"),
"No validation error that the object is not expected"
);
}

private static String resource(String name) {
return RESOURCE_PREFIX + name;
}
}
1 change: 1 addition & 0 deletions src/test/resources/issues/662/emptyObject.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions src/test/resources/issues/662/objectInvalidValue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"optionalObject": {
"value": "invalid"
}
}
26 changes: 26 additions & 0 deletions src/test/resources/issues/662/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"optionalObject": {
"anyOf": [
{
"type": "null"
},
{
"type": "object",
"properties": {
"value": {
"enum": [
"one",
"two"
]
}
},
"required": [
"value"
]
}
]
}
}
}
5 changes: 5 additions & 0 deletions src/test/resources/issues/662/validObject.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"optionalObject": {
"value": "one"
}
}