Skip to content

Commit 211fc5d

Browse files
committed
making ReferenceResolver null-tolerant
1 parent 06eba66 commit 211fc5d

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ public final class ReferenceResolver {
3737
* @return the resolved URI
3838
*/
3939
public static URI resolve(final URI parentScope, final String encounteredSegment) {
40-
return parentScope.resolve(encounteredSegment);
40+
try {
41+
return new URI(resolve(parentScope == null ? null : parentScope.toString(),
42+
encounteredSegment));
43+
} catch (URISyntaxException e) {
44+
throw new RuntimeException(e);
45+
}
4146
}
4247

4348
/**
@@ -52,11 +57,13 @@ public static URI resolve(final URI parentScope, final String encounteredSegment
5257
*/
5358
public static String resolve(final String parentScope, final String encounteredSegment) {
5459
try {
60+
if (parentScope == null) {
61+
return encounteredSegment;
62+
}
5563
return new URI(parentScope).resolve(encounteredSegment).toString();
5664
} catch (URISyntaxException e) {
5765
throw new RuntimeException(e);
5866
}
59-
// return new ReferenceResolver(parentScope, encounteredSegment).resolve();
6067
}
6168

6269
private ReferenceResolver() {

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.everit.json.schema.loader.internal;
1717

18+
import java.net.URI;
19+
import java.net.URISyntaxException;
1820
import java.util.Arrays;
1921
import java.util.List;
2022

@@ -38,7 +40,8 @@ public static List<Object[]> params() {
3840
parList("file name after folder path", "http://x.y.z/schema/child.json",
3941
"http://x.y.z/schema/", "child.json"),
4042
parList("new root", "http://bserver.com", "http://aserver.com/",
41-
"http://bserver.com"));
43+
"http://bserver.com"),
44+
parList("null parent", "http://a.b.c", null, "http://a.b.c"));
4245
}
4346

4447
private static Object[] parList(final String... params) {
@@ -65,4 +68,15 @@ public void test() {
6568
Assert.assertEquals(expectedOutput, actual);
6669
}
6770

71+
@Test
72+
public void testURI() {
73+
URI parentScopeURI;
74+
try {
75+
parentScopeURI = new URI(parentScope);
76+
} catch (URISyntaxException | NullPointerException e) {
77+
parentScopeURI = null;
78+
}
79+
URI actual = ReferenceResolver.resolve(parentScopeURI, encounteredSegment);
80+
}
81+
6882
}

0 commit comments

Comments
 (0)