Skip to content

Commit

Permalink
making ReferenceResolver null-tolerant
Browse files Browse the repository at this point in the history
  • Loading branch information
erosb committed Apr 10, 2016
1 parent 06eba66 commit 211fc5d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ public final class ReferenceResolver {
* @return the resolved URI
*/
public static URI resolve(final URI parentScope, final String encounteredSegment) {
return parentScope.resolve(encounteredSegment);
try {
return new URI(resolve(parentScope == null ? null : parentScope.toString(),
encounteredSegment));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

/**
Expand All @@ -52,11 +57,13 @@ public static URI resolve(final URI parentScope, final String encounteredSegment
*/
public static String resolve(final String parentScope, final String encounteredSegment) {
try {
if (parentScope == null) {
return encounteredSegment;
}
return new URI(parentScope).resolve(encounteredSegment).toString();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
// return new ReferenceResolver(parentScope, encounteredSegment).resolve();
}

private ReferenceResolver() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.everit.json.schema.loader.internal;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;

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

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

@Test
public void testURI() {
URI parentScopeURI;
try {
parentScopeURI = new URI(parentScope);
} catch (URISyntaxException | NullPointerException e) {
parentScopeURI = null;
}
URI actual = ReferenceResolver.resolve(parentScopeURI, encounteredSegment);
}

}

0 comments on commit 211fc5d

Please sign in to comment.