Skip to content
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

Fix schema location with hash in fragment #1075

Merged
merged 1 commit into from
Jun 24, 2024
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
14 changes: 8 additions & 6 deletions src/main/java/com/networknt/schema/SchemaLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,16 @@ public static SchemaLocation of(String iri) {
if ("#".equals(iri)) {
return DOCUMENT;
}
String[] iriParts = iri.split("#");
AbsoluteIri absoluteIri = null;
JsonNodePath fragment = JSON_POINTER;
if (iriParts.length > 0) {
absoluteIri = AbsoluteIri.of(iriParts[0]);
}
if (iriParts.length > 1) {
fragment = Fragment.of(iriParts[1]);
int index = iri.indexOf('#');
if (index == -1) {
absoluteIri = AbsoluteIri.of(iri);
} else {
absoluteIri = AbsoluteIri.of(iri.substring(0, index));
if (iri.length() > index + 1) {
fragment = Fragment.of(iri.substring(index + 1));
}
}
return new SchemaLocation(absoluteIri, fragment);
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/networknt/schema/SchemaLocationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,28 @@ void hashCodeEquals() {
SchemaLocation.of("https://example.com/schemas/address/#street_address").hashCode());
}

@Test
void hashInFragment() {
SchemaLocation location = SchemaLocation.of("https://example.com/example.yaml#/paths/~1subscribe/post/callbacks/myEvent/{request.body#~1callbackUrl}/post/requestBody/content/application~1json/schema");
assertEquals("/paths/~1subscribe/post/callbacks/myEvent/{request.body#~1callbackUrl}/post/requestBody/content/application~1json/schema", location.getFragment().toString());
}

@Test
void trailingHash() {
SchemaLocation location = SchemaLocation.of("https://example.com/example.yaml#");
assertEquals("", location.getFragment().toString());
}

@Test
void equalsEqualsNoFragment() {
assertEquals(SchemaLocation.of("https://example.com/example.yaml#"),
SchemaLocation.of("https://example.com/example.yaml"));
}

@Test
void equalsEqualsNoFragmentToString() {
assertEquals(SchemaLocation.of("https://example.com/example.yaml#").toString(),
SchemaLocation.of("https://example.com/example.yaml").toString());
}

}