Skip to content

Commit 367d973

Browse files
committed
adding checking of actual type of subject in ValidatingVisitor ctor to avoid confusion on handled types
1 parent 239816f commit 367d973

File tree

4 files changed

+78
-15
lines changed

4 files changed

+78
-15
lines changed

core/src/main/java/org/everit/json/schema/ObjectComparator.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.everit.json.schema;
22

3-
import org.json.JSONArray;
4-
import org.json.JSONObject;
5-
63
import java.util.Arrays;
74
import java.util.Objects;
85

6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
99
/**
1010
* Deep-equals implementation on primitive wrappers, {@link JSONObject} and {@link JSONArray}.
1111
*/
@@ -14,11 +14,13 @@ public final class ObjectComparator {
1414
/**
1515
* Deep-equals implementation on primitive wrappers, {@link JSONObject} and {@link JSONArray}.
1616
*
17-
* @param obj1 the first object to be inspected
18-
* @param obj2 the second object to be inspected
17+
* @param obj1
18+
* the first object to be inspected
19+
* @param obj2
20+
* the second object to be inspected
1921
* @return {@code true} if the two objects are equal, {@code false} otherwise
2022
*/
21-
public static boolean deepEquals(final Object obj1, final Object obj2) {
23+
public static boolean deepEquals(Object obj1, Object obj2) {
2224
if (obj1 instanceof JSONArray) {
2325
if (!(obj2 instanceof JSONArray)) {
2426
return false;
@@ -33,7 +35,7 @@ public static boolean deepEquals(final Object obj1, final Object obj2) {
3335
return Objects.equals(obj1, obj2);
3436
}
3537

36-
private static boolean deepEqualArrays(final JSONArray arr1, final JSONArray arr2) {
38+
private static boolean deepEqualArrays(JSONArray arr1, JSONArray arr2) {
3739
if (arr1.length() != arr2.length()) {
3840
return false;
3941
}
@@ -45,7 +47,7 @@ private static boolean deepEqualArrays(final JSONArray arr1, final JSONArray arr
4547
return true;
4648
}
4749

48-
private static String[] sortedNamesOf(final JSONObject obj) {
50+
private static String[] sortedNamesOf(JSONObject obj) {
4951
String[] raw = JSONObject.getNames(obj);
5052
if (raw == null) {
5153
return null;
@@ -54,7 +56,7 @@ private static String[] sortedNamesOf(final JSONObject obj) {
5456
return raw;
5557
}
5658

57-
private static boolean deepEqualObjects(final JSONObject jsonObj1, final JSONObject jsonObj2) {
59+
private static boolean deepEqualObjects(JSONObject jsonObj1, JSONObject jsonObj2) {
5860
String[] obj1Names = sortedNamesOf(jsonObj1);
5961
if (!Arrays.equals(obj1Names, sortedNamesOf(jsonObj2))) {
6062
return false;

core/src/main/java/org/everit/json/schema/ValidatingVisitor.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
package org.everit.json.schema;
22

33
import static java.lang.String.format;
4+
import static java.util.Arrays.asList;
5+
import static java.util.Collections.unmodifiableList;
6+
import static java.util.stream.Collectors.joining;
47
import static org.everit.json.schema.EnumSchema.toJavaValue;
58

69
import java.util.ArrayList;
710
import java.util.Collection;
811
import java.util.List;
912

13+
import org.json.JSONArray;
1014
import org.json.JSONObject;
1115

1216
class ValidatingVisitor extends Visitor {
1317

18+
private static final List<Class<?>> VALIDATED_TYPES = unmodifiableList(asList(
19+
Number.class,
20+
String.class,
21+
Boolean.class,
22+
JSONObject.class,
23+
JSONArray.class,
24+
JSONObject.NULL.getClass()
25+
));
26+
27+
static final String TYPE_FAILURE_MSG = "subject is an instance of non-handled type %s. Should be one of "
28+
+ VALIDATED_TYPES.stream().map(Class::getSimpleName).collect(joining(", "));
29+
1430
private static boolean isNull(Object obj) {
1531
return obj == null || JSONObject.NULL.equals(obj);
1632
}
@@ -31,6 +47,9 @@ void visit(Schema schema) {
3147
}
3248

3349
ValidatingVisitor(Object subject, ValidationFailureReporter failureReporter, ReadWriteValidator readWriteValidator) {
50+
if (subject != null && !VALIDATED_TYPES.stream().anyMatch(type -> type.isAssignableFrom(subject.getClass()))) {
51+
throw new IllegalArgumentException(format(TYPE_FAILURE_MSG, subject.getClass().getSimpleName()));
52+
}
3453
this.subject = subject;
3554
this.failureReporter = failureReporter;
3655
this.readWriteValidator = readWriteValidator;

core/src/test/java/org/everit/json/schema/EnumSchemaTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
import static org.junit.Assert.assertEquals;
2020

2121
import java.io.StringWriter;
22-
import java.util.HashMap;
2322
import java.util.ArrayList;
2423
import java.util.List;
25-
import java.util.Map;
2624
import java.util.stream.Collectors;
2725
import java.util.stream.IntStream;
2826

@@ -79,10 +77,8 @@ public void objectInArrayMatches() {
7977
possibleValues.add(arr);
8078

8179
EnumSchema subject = subject().build();
82-
Map<String, Object> map = new HashMap<>();
83-
map.put("a", true);
84-
List<Object> list = asList(map);
85-
subject.validate(list);
80+
81+
subject.validate(new JSONArray("[{\"a\":true}]"));
8682
}
8783

8884
private List<Object> asSet(final JSONArray array) {

core/src/test/java/org/everit/json/schema/ValidatingVisitorTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@
66
import static org.mockito.Mockito.verify;
77
import static org.mockito.Mockito.verifyZeroInteractions;
88

9+
import java.math.BigDecimal;
10+
import java.math.BigInteger;
11+
import java.util.ArrayList;
12+
13+
import org.json.JSONArray;
914
import org.json.JSONObject;
1015
import org.junit.Before;
1116
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
19+
import junitparams.JUnitParamsRunner;
20+
import junitparams.Parameters;
1221

22+
@RunWith(JUnitParamsRunner.class)
1323
public class ValidatingVisitorTest {
1424

1525
private ValidationFailureReporter reporter;
@@ -68,4 +78,40 @@ public void passesTypeCheck_sameType() {
6878
verifyZeroInteractions(reporter);
6979
}
7080

81+
public Object[] permittedTypes() {
82+
return new Object[] {
83+
new Object[] { "str" },
84+
new Object[] { 1 },
85+
new Object[] { 1L },
86+
new Object[] { 1.0 },
87+
new Object[] { 1.0f },
88+
new Object[] { new BigInteger("42") },
89+
new Object[] { new BigDecimal("42.3") },
90+
new Object[] { true },
91+
new Object[] { null },
92+
new Object[] { JSONObject.NULL },
93+
new Object[] { new JSONObject("{}") },
94+
new Object[] { new JSONArray("[]") },
95+
};
96+
}
97+
98+
public Object[] notPermittedTypes() {
99+
return new Object[] {
100+
new Object[] { new ArrayList<String>() },
101+
new Object[] { new RuntimeException() }
102+
};
103+
}
104+
105+
@Test
106+
@Parameters(method = "permittedTypes")
107+
public void permittedTypeSuccess(Object subject) {
108+
new ValidatingVisitor(subject, reporter, ReadWriteValidator.NONE);
109+
}
110+
111+
@Test(expected = IllegalArgumentException.class)
112+
@Parameters(method = "notPermittedTypes")
113+
public void notPermittedTypeFailure(Object subject) {
114+
new ValidatingVisitor(subject, reporter, ReadWriteValidator.NONE);
115+
}
116+
71117
}

0 commit comments

Comments
 (0)