Skip to content

Commit

Permalink
Fix schema location with hash in fragment (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay authored Jun 24, 2024
1 parent 7c518f6 commit 7a58366
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
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());
}

}

0 comments on commit 7a58366

Please sign in to comment.