Skip to content

Commit 02f8c32

Browse files
committed
further improvements in URI resolution, according to the discussion in #17
1 parent d36081d commit 02f8c32

File tree

6 files changed

+25
-4
lines changed

6 files changed

+25
-4
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ private String handlePathIdAttr() {
6464
if (parentScopeURL.getPort() > -1) {
6565
newIdBuilder.append(":").append(parentScopeURL.getPort());
6666
}
67-
newIdBuilder.append("/").append(encounteredSegment);
67+
newIdBuilder.append(removeTrailingSegmentFrom(parentScopeURL.getPath()))
68+
.append("/")
69+
.append(encounteredSegment);
6870
return newIdBuilder.toString();
6971
} catch (MalformedURLException e1) {
7072
return concat();
@@ -80,6 +82,13 @@ private String nonfragmentIdAttr() {
8082
}
8183
}
8284

85+
private String removeTrailingSegmentFrom(final String path) {
86+
if (path.isEmpty() || "/".equals(path)) {
87+
return "";
88+
}
89+
return path.substring(0, path.lastIndexOf('/'));
90+
}
91+
8392
private String resolve() {
8493
if (encounteredSegment.startsWith("#")) {
8594
return concat();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void pointerResolution() {
291291
ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("pointerResolution"));
292292
ObjectSchema rectangleSchema = (ObjectSchema) ((ReferenceSchema) actual.getPropertySchemas()
293293
.get("rectangle"))
294-
.getReferredSchema();
294+
.getReferredSchema();
295295
Assert.assertNotNull(rectangleSchema);
296296
ReferenceSchema aRef = (ReferenceSchema) rectangleSchema.getPropertySchemas().get("a");
297297
Assert.assertTrue(aRef.getReferredSchema() instanceof NumberSchema);
@@ -312,6 +312,8 @@ public void remotePointerResulion() {
312312
SchemaClient httpClient = Mockito.mock(SchemaClient.class);
313313
Mockito.when(httpClient.get("http://example.org/asd")).thenReturn(asStream("{}"));
314314
Mockito.when(httpClient.get("http://example.org/otherschema.json")).thenReturn(asStream("{}"));
315+
Mockito.when(httpClient.get("http://example.org/folder/subschemaInFolder.json")).thenReturn(
316+
asStream("{}"));
315317
SchemaLoader.load(get("remotePointerResolution"), httpClient);
316318
// Mockito.verify(httpClient);
317319
}

core/src/test/java/org/everit/json/schema/loader/internal/ReferenceResolverTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public static List<Object[]> params() {
3232
return Arrays.asList(
3333
parList("fragment id", "http://x.y.z/root.json#foo", "http://x.y.z/root.json", "#foo"),
3434
parList("rel path", "http://example.org/foo", "http://example.org/bar", "foo"),
35+
parList("file name change", "http://x.y.z/schema/child.json",
36+
"http://x.y.z/schema/parent.json",
37+
"child.json"),
38+
parList("file name after folder path", "http://x.y.z/schema/child.json",
39+
"http://x.y.z/schema/", "child.json"),
3540
parList("new root", "http://bserver.com", "http://aserver.com/",
3641
"http://bserver.com"));
3742
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@
275275
"properties" : {
276276
"prop" : {"$ref" : "otherschema.json"}
277277
}
278+
},
279+
"folderChange" : {
280+
"id" : "http://example.org/folder/",
281+
"properties" : {
282+
"schemaInFolder" : {"$ref" : "subschemaInFolder.json"}
283+
}
278284
}
279285
}
280286
}

tests/src/test/java/org/everit/json/schema/IntegrationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public static List<Object[]> params() {
5353
new ResourcesScanner());
5454
Set<String> paths = refs.getResources(Pattern.compile(".*\\.json"));
5555
for (String path : paths) {
56-
if (path.indexOf("/optional/") > -1 || path.indexOf("/remotes/") > -1
57-
|| path.endsWith("refRemote.json")) {
56+
if (path.indexOf("/optional/") > -1 || path.indexOf("/remotes/") > -1) {
5857
continue;
5958
}
6059
String fileName = path.substring(path.lastIndexOf('/') + 1);
File renamed without changes.

0 commit comments

Comments
 (0)