Skip to content

Commit 5beae14

Browse files
committed
finishing and adding javadoc to ValidationException#toJSON()
1 parent 659b782 commit 5beae14

File tree

6 files changed

+77
-22
lines changed

6 files changed

+77
-22
lines changed

core/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2020
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2121
<modelVersion>4.0.0</modelVersion>
22-
<parent>
23-
<groupId>org.everit.config</groupId>
24-
<artifactId>org.everit.config.oss</artifactId>
25-
<version>7.2.0</version>
26-
</parent>
2722
<groupId>org.everit.json</groupId>
2823
<artifactId>org.everit.json.schema</artifactId>
2924
<version>1.4.0</version>

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ public String getMessage() {
273273
* @return the JSON pointer
274274
*/
275275
public String getPointerToViolation() {
276+
if (pointerToViolation == null) {
277+
return null;
278+
}
276279
return pointerToViolation.toString();
277280
}
278281

@@ -322,12 +325,37 @@ public String getKeyword() {
322325
return keyword;
323326
}
324327

328+
/**
329+
* Creates a JSON representation of the failure.
330+
*
331+
* The returned {@code JSONObject} contains the following keys:
332+
* <ul>
333+
* <li>{@code "message"}: a programmer-friendly exception message. This value is a non-nullable
334+
* string.</li>
335+
* <li>{@code "keyword"}: a JSON Schema keyword which was used in the schema and violated by the
336+
* input JSON. This value is a nullable string.</li>
337+
* <li>{@code "pointerToViolation"}: a JSON Pointer denoting the path from the root of the
338+
* document to the invalid fragment of it. This value is a non-nullable string. See
339+
* {@link #getPointerToViolation()}</li>
340+
* <li>{@code "causingExceptions"}: is a (possibly empty) array of violations which caused this
341+
* exceptions. See {@link #getCausingExceptions()}</li>
342+
* </ul>
343+
*
344+
* @return a JSON description of the validation error
345+
*/
325346
public JSONObject toJSON() {
326347
JSONObject rval = new JSONObject();
327348
rval.put("keyword", keyword);
328-
rval.put("pointerToViolation", getPointerToViolation());
329-
rval.put("message", getMessage());
330-
rval.put("causingExceptions", new JSONArray());
349+
if (pointerToViolation == null) {
350+
rval.put("pointerToViolation", JSONObject.NULL);
351+
} else {
352+
rval.put("pointerToViolation", getPointerToViolation());
353+
}
354+
rval.put("message", super.getMessage());
355+
List<JSONObject> causeJsons = causingExceptions.stream()
356+
.map(ValidationException::toJSON)
357+
.collect(Collectors.toList());
358+
rval.put("causingExceptions", new JSONArray(causeJsons));
331359
return rval;
332360
}
333361
}

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,39 @@ public void testToJSON() {
192192
ValidationException subject =
193193
new ValidationException(BooleanSchema.INSTANCE, new StringBuilder("#/a/b"),
194194
"exception message", Collections.emptyList(), "type");
195-
JSONObject expected = new JSONObject(new JSONTokener(
196-
getClass().getResourceAsStream("/org/everit/jsonvalidator/exception-to-json.json")));
195+
JSONObject expected = readFile("/org/everit/jsonvalidator/exception-to-json.json");
196+
JSONObject actual = subject.toJSON();
197+
Assert.assertTrue(ObjectComparator.deepEquals(expected, actual));
198+
}
199+
200+
private JSONObject readFile(final String absPath) {
201+
return new JSONObject(new JSONTokener(
202+
getClass().getResourceAsStream(absPath)));
203+
}
204+
205+
@Test
206+
public void toJSONNullPointerToViolation() {
207+
ValidationException subject =
208+
new ValidationException(BooleanSchema.INSTANCE, null,
209+
"exception message", Collections.emptyList(), "type");
210+
JSONObject actual = subject.toJSON();
211+
Assert.assertEquals(JSONObject.NULL, actual.get("pointerToViolation"));
212+
}
197213

214+
@Test
215+
public void toJSONWithCauses() {
216+
ValidationException cause =
217+
new ValidationException(NullSchema.INSTANCE,
218+
new StringBuilder("#/a/0"),
219+
"cause msg",
220+
Collections.emptyList(),
221+
"type");
222+
ValidationException subject =
223+
new ValidationException(BooleanSchema.INSTANCE, new StringBuilder("#/a"),
224+
"exception message", Arrays.asList(cause), "type");
225+
JSONObject expected = readFile("/org/everit/jsonvalidator/exception-to-json-with-causes.json");
198226
JSONObject actual = subject.toJSON();
199-
Assert.assertEquals(4, JSONObject.getNames(actual).length);
200-
Assert.assertEquals(expected.get("keyword"), actual.get("keyword"));
201-
Assert.assertEquals(expected.get("pointerToViolation"), actual.get("pointerToViolation"));
202-
Assert.assertEquals(expected.get("message"), actual.get("message"));
203-
// Assert.assertEquals(expected.getJSONArray("causingExceptions"),
204-
// actual.getJSONArray("causingExceptions"));
205-
Assert.assertTrue(ObjectComparator.deepEquals(expected.getJSONArray("causingExceptions"),
206-
actual.getJSONArray("causingExceptions")));
207-
Assert.assertTrue(ObjectComparator.deepEquals(expected,
208-
actual));
227+
Assert.assertTrue(ObjectComparator.deepEquals(expected, actual));
209228
}
210229

211230
}

core/src/test/resources/org/everit/jsonvalidator/arraytestcases.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"additionalItemsSchema" : [true, null, null],
1212
"additionalItemsSchemaFailure" : [true, null, false],
1313
"nonUniqueArrayOfArrays" : [["foo"], ["foo"]],
14-
"uniqueItemsWithSameToString" : [{"a":"b"}, '{"a":"b"}']
14+
"uniqueItemsWithSameToString" : [{"a":"b"}, "{\"a\":\"b\"}"]
1515
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"pointerToViolation" : "#/a",
3+
"message" : "exception message",
4+
"keyword" : "type",
5+
"causingExceptions" : [
6+
{
7+
"pointerToViolation" : "#/a/0",
8+
"message" : "cause msg",
9+
"keyword" : "type",
10+
"causingExceptions" : []
11+
}
12+
]
13+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"pointerToViolation" : "#/a/b",
3-
"message" : "#/a/b: exception message",
3+
"message" : "exception message",
44
"keyword" : "type",
55
"causingExceptions" : []
66
}

0 commit comments

Comments
 (0)