Skip to content

Fix relative iris with colons #1147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/main/java/com/networknt/schema/AbsoluteIri.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ protected String getSchemeAuthority() {
return getSchemeAuthority(this.value);
}

/**
* Determines if the iri is absolute or relative.
*
* @param iri to determine
* @return true if absolute
*/
private static boolean isAbsoluteIri(String iri) {
int length = iri.length();
for (int x = 0; x < length; x++) {
char ch = iri.charAt(x);
if (ch == ':') {
// if the first segment is relative with a colon it must be a dot path segment
// ie. ./foo:bar
return true;
} else if (ch == '/' || ch == '?' || ch == '#') {
return false;
}
}
return false;
}

/**
* Constructs a new IRI by parsing the given string and then resolving it
* against this IRI.
Expand All @@ -85,7 +106,7 @@ protected String getSchemeAuthority() {
* @return the new absolute IRI
*/
public static String resolve(String parent, String iri) {
if (iri.contains(":")) {
if (isAbsoluteIri(iri)) {
// IRI is absolute
return iri;
} else {
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/com/networknt/schema/AbsoluteIriTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ void resolveNull() {
assertEquals("test.json", iri.resolve("test.json").toString());
}

@Test
void relativeColonDotPathSegment() {
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
assertEquals("http://www.example.org/foo/foo:bar", iri.resolve("./foo:bar").toString());
}

@Test
void relativeColonSecondSegment() {
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
assertEquals("http://www.example.org/foo/bar/foo:bar", iri.resolve("bar/foo:bar").toString());
}

@Test
void relativeColonQueryString() {
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
assertEquals("http://www.example.org/foo/test.json?queryParam=foo:bar", iri.resolve("test.json?queryParam=foo:bar").toString());
}

@Test
void relativeColonAnchor() {
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
assertEquals("http://www.example.org/foo/test.json#foo:bar", iri.resolve("test.json#foo:bar").toString());
}

@Test
void relativeAtDocument() {
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
Expand Down Expand Up @@ -93,6 +117,18 @@ void relativeParentWithSchemeSpecificPart() {
assertEquals("classpath:resource/test.json", iri.resolve("../../test.json").toString());
}

@Test
void rootColonDotPathSegment() {
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
assertEquals("http://www.example.org/foo:bar", iri.resolve("/foo:bar").toString());
}

@Test
void rootColonSecondSegment() {
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
assertEquals("http://www.example.org/bar/foo:bar", iri.resolve("/bar/foo:bar").toString());
}

@Test
void rootAbsoluteAtDocument() {
AbsoluteIri iri = new AbsoluteIri("http://www.example.org/foo/bar.json");
Expand Down
Loading