Repository: networknt/json-schema-validator
Pull request #1075 (2024-06-24 12:43:18)

Fix schema location with hash in fragment
This fixes the case where the schema location has a `#` in the fragment for example `https://example.com/example.yaml#/paths/~1subscribe/post/callbacks/myEvent/{request.body#~1callbackUrl}/post/requestBody/content/application~1json/schema`.

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/networknt/json-schema-validator/pull/1075?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=networknt) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 77.94%. Comparing base [(`48ca3c2`)](https://app.codecov.io/gh/networknt/json-schema-validator/commit/48ca3c22995e32797e9defdb8b77869f0d15caf3?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=networknt) to head [(`38a0b3b`)](https://app.codecov.io/gh/networknt/json-schema-validator/commit/38a0b3b916b37ac248be0db4b4fc3fb44797a9c2?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=networknt).
> Report is 40 commits behind head on master.

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #1075      +/-   ##
============================================
- Coverage     78.90%   77.94%   -0.96%     
- Complexity     1965     2049      +84     
============================================
  Files           172      198      +26     
  Lines          6352     6851     +499     
  Branches       1255     1255              
============================================
+ Hits           5012     5340     +328     
- Misses          867     1032     +165     
- Partials        473      479       +6     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/networknt/json-schema-validator/pull/1075?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=networknt).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=networknt).


Patch:
diff --git a/src/main/java/com/networknt/schema/SchemaLocation.java b/src/main/java/com/networknt/schema/SchemaLocation.java
index 7fa564b..d1b5f33 100644
--- a/src/main/java/com/networknt/schema/SchemaLocation.java
+++ b/src/main/java/com/networknt/schema/SchemaLocation.java
@@ -114,14 +114,16 @@ public class SchemaLocation {
         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);
     }


Test patch:
diff --git a/src/test/java/com/networknt/schema/SchemaLocationTest.java b/src/test/java/com/networknt/schema/SchemaLocationTest.java
index fabdb6a..c2b29d3 100644
--- a/src/test/java/com/networknt/schema/SchemaLocationTest.java
+++ b/src/test/java/com/networknt/schema/SchemaLocationTest.java
@@ -231,4 +231,28 @@ class SchemaLocationTest {
                 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());
+    }
+
 }