From 38a0b3b916b37ac248be0db4b4fc3fb44797a9c2 Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Mon, 24 Jun 2024 17:21:07 +0800 Subject: [PATCH] Fix schema location with hash in fragment --- .../com/networknt/schema/SchemaLocation.java | 14 ++++++----- .../networknt/schema/SchemaLocationTest.java | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/networknt/schema/SchemaLocation.java b/src/main/java/com/networknt/schema/SchemaLocation.java index 7fa564be0..d1b5f33b9 100644 --- a/src/main/java/com/networknt/schema/SchemaLocation.java +++ b/src/main/java/com/networknt/schema/SchemaLocation.java @@ -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); } diff --git a/src/test/java/com/networknt/schema/SchemaLocationTest.java b/src/test/java/com/networknt/schema/SchemaLocationTest.java index fabdb6ac1..c2b29d3a8 100644 --- a/src/test/java/com/networknt/schema/SchemaLocationTest.java +++ b/src/test/java/com/networknt/schema/SchemaLocationTest.java @@ -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()); + } + }