Repository: networknt/json-schema-validator
Pull request #350 (2020-11-20 20:50:39)

Add builder method that accepts iterable
This lets me do something like this:

```java
JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7))
            .uriFetcher(new JenkinsWorkspaceFileFetcher(), JenkinsWorkspaceFileFetcher.SUPPORTED_SCHEMES)
```

`JenkinsWorkspaceFileFetcher.SUPPORTED_SCHEMES` is a `Set<String>`, which can't be passed into varargs. But varargs can be passed into an `Iterable<String>` if you wrap it with `Arrays.asList(varargs)`, and it is cheap too since an `ArrayList<>` is just backed by an array.


Patch:
diff --git a/src/main/java/com/networknt/schema/JsonSchema.java b/src/main/java/com/networknt/schema/JsonSchema.java
index be5bb64..a12b3bd 100644
--- a/src/main/java/com/networknt/schema/JsonSchema.java
+++ b/src/main/java/com/networknt/schema/JsonSchema.java
@@ -95,7 +95,7 @@ public class JsonSchema extends BaseJsonValidator {
             try {
                 return this.validationContext.getURIFactory().create(currentUri, id);
             } catch (IllegalArgumentException e) {
-                throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.ID.getValue(), ValidatorTypeCode.ID, id, currentUri.toString()));
+                throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.ID.getValue(), ValidatorTypeCode.ID, id, currentUri == null ? "null" : currentUri.toString()));
             }
         }
     }
diff --git a/src/main/java/com/networknt/schema/JsonSchemaFactory.java b/src/main/java/com/networknt/schema/JsonSchemaFactory.java
index 872d01f..38e873e 100644
--- a/src/main/java/com/networknt/schema/JsonSchemaFactory.java
+++ b/src/main/java/com/networknt/schema/JsonSchemaFactory.java
@@ -27,6 +27,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
@@ -88,6 +89,10 @@ public class JsonSchemaFactory {
          * @return this builder.
          */
         public Builder uriFactory(final URIFactory uriFactory, final String... schemes) {
+            return uriFactory(uriFactory, Arrays.asList(schemes));
+        }
+
+        public Builder uriFactory(final URIFactory uriFactory, final Iterable<String> schemes) {
             for (final String scheme : schemes) {
                 this.uriFactoryMap.put(scheme, uriFactory);
             }
@@ -102,6 +107,10 @@ public class JsonSchemaFactory {
          * @return this builder.
          */
         public Builder uriFetcher(final URIFetcher uriFetcher, final String... schemes) {
+            return uriFetcher(uriFetcher, Arrays.asList(schemes));
+        }
+
+        public Builder uriFetcher(final URIFetcher uriFetcher, final Iterable<String> schemes) {
             for (final String scheme : schemes) {
                 this.uriFetcherMap.put(scheme, uriFetcher);
             }


Test patch:
diff --git a/src/test/java/com/networknt/schema/Issue347Test.java b/src/test/java/com/networknt/schema/Issue347Test.java
new file mode 100644
index 0000000..d110d65
--- /dev/null
+++ b/src/test/java/com/networknt/schema/Issue347Test.java
@@ -0,0 +1,22 @@
+package com.networknt.schema;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class Issue347Test {
+
+    @Test
+    public void failure() {
+        ObjectMapper mapper = new ObjectMapper();
+        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
+        try {
+            JsonSchema schema = factory.getSchema(Thread.currentThread().getContextClassLoader().getResourceAsStream("schema/issue347-v7.json"));
+        } catch (Throwable e) {
+            assertThat(e, instanceOf(JsonSchemaException.class));
+            assertEquals("test: null is an invalid segment for URI {2}", e.getMessage());
+        }
+    }
+}
diff --git a/src/test/resources/schema/issue347-v7.json b/src/test/resources/schema/issue347-v7.json
new file mode 100644
index 0000000..2ae91c0
--- /dev/null
+++ b/src/test/resources/schema/issue347-v7.json
@@ -0,0 +1,19 @@
+{
+  "$id": "test",
+  "$schema": "http://json-schema.org/draft-07/schema#",
+  "description": "Test description",
+  "properties": {
+    "test": {
+      "description": "Sample property",
+      "examples": [
+        "hello",
+        "world"
+      ],
+      "type": "string"
+    }
+  },
+  "required": [
+    "test"
+  ],
+  "type": "object"
+}