Skip to content

Commit 3bb3f05

Browse files
committed
using the org.json.JSONPointer in existing JSONPointer class, added new testcase
1 parent 4464950 commit 3bb3f05

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

core/src/main/java/org/everit/json/schema/loader/internal/JSONPointer.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
import org.everit.json.schema.SchemaException;
2828
import org.everit.json.schema.loader.SchemaClient;
29-
import org.json.JSONArray;
3029
import org.json.JSONException;
3130
import org.json.JSONObject;
31+
import org.json.JSONPointerException;
3232
import org.json.JSONTokener;
3333

3434
/**
@@ -167,25 +167,26 @@ public QueryResult query() {
167167
if ((path[0] == null) || !path[0].startsWith("#")) {
168168
throw new IllegalArgumentException("JSON pointers must start with a '#'");
169169
}
170-
Object current = document;
171-
for (int i = 1; i < path.length; ++i) {
172-
String segment = unescape(path[i]);
173-
if (current instanceof JSONObject) {
174-
if (!((JSONObject) current).has(segment)) {
175-
throw new SchemaException(String.format(
176-
"failed to resolve JSON pointer [%s]. Segment [%s] not found in %s", fragment,
177-
segment, document.toString()));
178-
}
179-
current = ((JSONObject) current).get(segment);
180-
} else if (current instanceof JSONArray) {
181-
current = ((JSONArray) current).get(Integer.parseInt(segment));
182-
}
170+
try {
171+
JSONObject result = queryFrom(document);
172+
return new QueryResult(document, result);
173+
} catch (JSONPointerException e) {
174+
throw new SchemaException(e.getMessage());
183175
}
184-
return new QueryResult(document, (JSONObject) current);
185176
}
186177

187-
private String unescape(final String segment) {
188-
return segment.replace("~1", "/").replace("~0", "~").replace("%25", "%");
178+
private JSONObject queryFrom(final JSONObject document) {
179+
JSONObject result; // temporary workaround
180+
if ("#".equals(fragment)) {
181+
result = document;
182+
} else {
183+
result = (JSONObject) new org.json.JSONPointer(fragment).queryFrom(document);
184+
}
185+
if (result == null) {
186+
throw new JSONPointerException(
187+
String.format("could not query schema document by pointer [%s]", fragment));
188+
}
189+
return result;
189190
}
190191

191192
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ public void pointerResolutionFailure() {
340340
SchemaLoader.load(get("pointerResolutionFailure"));
341341
}
342342

343+
@Test(expected = SchemaException.class)
344+
public void pointerResolutionQueryFailure() {
345+
SchemaLoader.load(get("pointerResolutionQueryFailure"));
346+
}
347+
343348
@Test
344349
public void propsAroundRefExtendTheReferredSchema() {
345350
ObjectSchema actual = (ObjectSchema) SchemaLoader

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@
154154
},
155155
"definitions" : {}
156156
},
157+
"pointerResolutionQueryFailure" : {
158+
"type" : "object",
159+
"properties" : {
160+
"rectangle" : {"$ref" : "#/definitions/nested/Rectangle" }
161+
},
162+
"definitions" : {}
163+
},
157164
"arrayByItems" : {
158165
"items" : []
159166
},

0 commit comments

Comments
 (0)