Skip to content

Commit d3ec64e

Browse files
committed
adding unittest for exception handling for invalid URIs
* also adding `SchemaException#toJSON()` with `"schemaLocation"` and `"message"` keys
1 parent 6373def commit d3ec64e

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.Collection;
99
import java.util.List;
1010

11+
import org.json.JSONObject;
12+
1113
/**
1214
* Thrown by {@link org.everit.json.schema.loader.SchemaLoader#load()} when it encounters
1315
* un-parseable schema JSON definition.
@@ -76,6 +78,11 @@ public SchemaException(String schemaLocation, Class<?> actualType, Collection<Cl
7678
this.schemaLocation = schemaLocation;
7779
}
7880

81+
public SchemaException(String schemaLocation, Exception cause) {
82+
super(cause.getMessage(), cause);
83+
this.schemaLocation = schemaLocation;
84+
}
85+
7986
@Deprecated
8087
public SchemaException(String message) {
8188
this((String) null, message);
@@ -118,4 +125,11 @@ public SchemaException(String message, Throwable cause) {
118125
public String getSchemaLocation() {
119126
return schemaLocation;
120127
}
128+
129+
public JSONObject toJSON() {
130+
JSONObject rval = new JSONObject();
131+
rval.put("schemaLocation", schemaLocation);
132+
rval.put("message", getMessage());
133+
return rval;
134+
}
121135
}

core/src/main/java/org/everit/json/schema/loader/JsonPointerEvaluator.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,10 @@ static final JsonPointerEvaluator forDocument(JsonObject document, String fragme
109109
}
110110

111111
private static JsonObject configureBasedOnState(JsonObject obj, LoadingState callingState, String id) {
112-
try {
113-
URI uri = new URI(id);
114-
obj.ls = new LoadingState(callingState.config, callingState.pointerSchemas, obj, obj, uri, SchemaLocation.empty());
115-
} catch (URISyntaxException e) {
116-
throw callingState.createSchemaException("invalid URI: " + e.getMessage());
117-
}
112+
obj.ls = new LoadingState(callingState.config,
113+
callingState.pointerSchemas, obj, obj,
114+
validateURI(callingState, id),
115+
SchemaLocation.empty());
118116
return obj;
119117
}
120118

@@ -129,10 +127,19 @@ static final JsonPointerEvaluator forURL(SchemaClient schemaClient, String url,
129127
fragment = url.substring(poundIdx);
130128
toBeQueried = url.substring(0, poundIdx);
131129
}
130+
validateURI(callingState, toBeQueried);
132131
return new JsonPointerEvaluator(() -> configureBasedOnState(executeWith(schemaClient, toBeQueried), callingState, toBeQueried),
133132
fragment);
134133
}
135134

135+
private static URI validateURI(LoadingState callingState, String toBeQueried) {
136+
try {
137+
return new URI(toBeQueried);
138+
} catch (URISyntaxException e) {
139+
throw callingState.createSchemaException(e);
140+
}
141+
}
142+
136143
private final Supplier<JsonObject> documentProvider;
137144

138145
private final String fragment;

core/src/main/java/org/everit/json/schema/loader/LoadingState.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ SchemaException createSchemaException(String message) {
145145
return new SchemaException(locationOfCurrentObj(), message);
146146
}
147147

148+
SchemaException createSchemaException(Exception cause) {
149+
return new SchemaException(locationOfCurrentObj(), cause);
150+
}
151+
148152
SchemaException createSchemaException(Class<?> actualType, Class<?> expectedType, Class<?>... furtherExpectedTypes) {
149153
return new SchemaException(locationOfCurrentObj(), actualType, expectedType, furtherExpectedTypes);
150154
}

core/src/test/java/org/everit/json/schema/loader/JsonPointerEvaluatorTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import static java.util.Arrays.asList;
44
import static java.util.Collections.emptyMap;
5+
import static org.everit.json.schema.JSONMatcher.sameJsonAs;
56
import static org.everit.json.schema.TestSupport.asStream;
67
import static org.everit.json.schema.loader.JsonValueTest.withLs;
78
import static org.junit.Assert.assertEquals;
89
import static org.junit.Assert.assertSame;
10+
import static org.junit.Assert.assertThat;
11+
import static org.junit.Assert.fail;
912
import static org.mockito.Mockito.mock;
1013
import static org.mockito.Mockito.when;
1114

@@ -24,7 +27,10 @@ public class JsonPointerEvaluatorTest {
2427
private static final JsonObject rootSchemaJson = withLs(JsonValue.of(ResourceLoader.DEFAULT.readObj("testschemas.json")
2528
.getJSONObject("refPointerDerivatedFromPointer").toMap())).requireObject();
2629

30+
private static final ResourceLoader LOADER = ResourceLoader.DEFAULT;
31+
2732
@Test
33+
2834
public void sameDocumentSuccess() {
2935
JsonPointerEvaluator pointer = JsonPointerEvaluator.forDocument(rootSchemaJson, "#/definitions/Bar");
3036
JsonObject actual = pointer.query().getQueryResult().requireObject();
@@ -77,14 +83,29 @@ public void remoteDocumentSuccess() {
7783
SchemaClient schemaClient = mock(SchemaClient.class);
7884
when(schemaClient.get("http://localhost:1234/hello")).thenReturn(rootSchemaJsonAsStream());
7985
JsonPointerEvaluator pointer = JsonPointerEvaluator
80-
.forURL(schemaClient, "http://localhost:1234/hello#/definitions/Bar", createLoadingState(schemaClient, "#/definitions/Foo"));
86+
.forURL(schemaClient, "http://localhost:1234/hello#/definitions/Bar",
87+
createLoadingState(schemaClient, "#/definitions/Foo"));
8188
JsonObject actual = pointer.query().getQueryResult().requireObject();
8289
assertEquals("dummy schema at #/definitions/Bar", actual.require("description").requireString());
8390
assertEquals("http://localhost:1234/folder/", actual.ls.id.toString());
8491
assertEquals(new SchemaLocation(asList("definitions", "Bar")), actual.ls.pointerToCurrentObj);
8592
}
8693

94+
@Test
95+
public void schemaExceptionForInvalidURI() {
96+
try {
97+
SchemaClient schemaClient = mock(SchemaClient.class);
98+
JsonPointerEvaluator subject = JsonPointerEvaluator.forURL(schemaClient, "||||",
99+
createLoadingState(schemaClient, "#/definitions/Foo"));
100+
subject.query();
101+
fail("did not throw exception");
102+
} catch (SchemaException e) {
103+
assertThat(e.toJSON(), sameJsonAs(LOADER.readObj("pointer-eval-non-uri-failure.json")));
104+
}
105+
}
106+
87107
protected InputStream rootSchemaJsonAsStream() {
88108
return asStream(new JSONObject(rootSchemaJson.toMap()).toString());
89109
}
90110
}
111+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"schemaLocation": "#",
3+
"message": "Illegal character in path at index 0: ||||"
4+
}

0 commit comments

Comments
 (0)