Skip to content

Commit 6c37935

Browse files
authored
Fix relative iris with colons (#1147)
1 parent 77c91a2 commit 6c37935

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/main/java/com/networknt/schema/AbsoluteIri.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ protected String getSchemeAuthority() {
7676
return getSchemeAuthority(this.value);
7777
}
7878

79+
/**
80+
* Determines if the iri is absolute or relative.
81+
*
82+
* @param iri to determine
83+
* @return true if absolute
84+
*/
85+
private static boolean isAbsoluteIri(String iri) {
86+
int length = iri.length();
87+
for (int x = 0; x < length; x++) {
88+
char ch = iri.charAt(x);
89+
if (ch == ':') {
90+
// if the first segment is relative with a colon it must be a dot path segment
91+
// ie. ./foo:bar
92+
return true;
93+
} else if (ch == '/' || ch == '?' || ch == '#') {
94+
return false;
95+
}
96+
}
97+
return false;
98+
}
99+
79100
/**
80101
* Constructs a new IRI by parsing the given string and then resolving it
81102
* against this IRI.
@@ -85,7 +106,7 @@ protected String getSchemeAuthority() {
85106
* @return the new absolute IRI
86107
*/
87108
public static String resolve(String parent, String iri) {
88-
if (iri.contains(":")) {
109+
if (isAbsoluteIri(iri)) {
89110
// IRI is absolute
90111
return iri;
91112
} else {

src/test/java/com/networknt/schema/AbsoluteIriTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ void resolveNull() {
3333
assertEquals("test.json", iri.resolve("test.json").toString());
3434
}
3535

36+
@Test
37+
void relativeColonDotPathSegment() {
38+
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
39+
assertEquals("http://www.example.org/foo/foo:bar", iri.resolve("./foo:bar").toString());
40+
}
41+
42+
@Test
43+
void relativeColonSecondSegment() {
44+
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
45+
assertEquals("http://www.example.org/foo/bar/foo:bar", iri.resolve("bar/foo:bar").toString());
46+
}
47+
48+
@Test
49+
void relativeColonQueryString() {
50+
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
51+
assertEquals("http://www.example.org/foo/test.json?queryParam=foo:bar", iri.resolve("test.json?queryParam=foo:bar").toString());
52+
}
53+
54+
@Test
55+
void relativeColonAnchor() {
56+
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
57+
assertEquals("http://www.example.org/foo/test.json#foo:bar", iri.resolve("test.json#foo:bar").toString());
58+
}
59+
3660
@Test
3761
void relativeAtDocument() {
3862
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
@@ -93,6 +117,18 @@ void relativeParentWithSchemeSpecificPart() {
93117
assertEquals("classpath:resource/test.json", iri.resolve("../../test.json").toString());
94118
}
95119

120+
@Test
121+
void rootColonDotPathSegment() {
122+
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
123+
assertEquals("http://www.example.org/foo:bar", iri.resolve("/foo:bar").toString());
124+
}
125+
126+
@Test
127+
void rootColonSecondSegment() {
128+
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
129+
assertEquals("http://www.example.org/bar/foo:bar", iri.resolve("/bar/foo:bar").toString());
130+
}
131+
96132
@Test
97133
void rootAbsoluteAtDocument() {
98134
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");

0 commit comments

Comments
 (0)