Skip to content

Commit 7a58366

Browse files
authored
Fix schema location with hash in fragment (#1075)
1 parent 7c518f6 commit 7a58366

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,16 @@ public static SchemaLocation of(String iri) {
114114
if ("#".equals(iri)) {
115115
return DOCUMENT;
116116
}
117-
String[] iriParts = iri.split("#");
118117
AbsoluteIri absoluteIri = null;
119118
JsonNodePath fragment = JSON_POINTER;
120-
if (iriParts.length > 0) {
121-
absoluteIri = AbsoluteIri.of(iriParts[0]);
122-
}
123-
if (iriParts.length > 1) {
124-
fragment = Fragment.of(iriParts[1]);
119+
int index = iri.indexOf('#');
120+
if (index == -1) {
121+
absoluteIri = AbsoluteIri.of(iri);
122+
} else {
123+
absoluteIri = AbsoluteIri.of(iri.substring(0, index));
124+
if (iri.length() > index + 1) {
125+
fragment = Fragment.of(iri.substring(index + 1));
126+
}
125127
}
126128
return new SchemaLocation(absoluteIri, fragment);
127129
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,28 @@ void hashCodeEquals() {
231231
SchemaLocation.of("https://example.com/schemas/address/#street_address").hashCode());
232232
}
233233

234+
@Test
235+
void hashInFragment() {
236+
SchemaLocation location = SchemaLocation.of("https://example.com/example.yaml#/paths/~1subscribe/post/callbacks/myEvent/{request.body#~1callbackUrl}/post/requestBody/content/application~1json/schema");
237+
assertEquals("/paths/~1subscribe/post/callbacks/myEvent/{request.body#~1callbackUrl}/post/requestBody/content/application~1json/schema", location.getFragment().toString());
238+
}
239+
240+
@Test
241+
void trailingHash() {
242+
SchemaLocation location = SchemaLocation.of("https://example.com/example.yaml#");
243+
assertEquals("", location.getFragment().toString());
244+
}
245+
246+
@Test
247+
void equalsEqualsNoFragment() {
248+
assertEquals(SchemaLocation.of("https://example.com/example.yaml#"),
249+
SchemaLocation.of("https://example.com/example.yaml"));
250+
}
251+
252+
@Test
253+
void equalsEqualsNoFragmentToString() {
254+
assertEquals(SchemaLocation.of("https://example.com/example.yaml#").toString(),
255+
SchemaLocation.of("https://example.com/example.yaml").toString());
256+
}
257+
234258
}

0 commit comments

Comments
 (0)