-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
URL mappings allows public, normally internet accessible URLs (such as might be externally referenced in a schema) to be mapped to local URLs that might be on the filesystem, at a different external URL, or embedded within a JAR file. This allows the JsonSchema validator to validate a schema with an external reference to validate against the local copy of that reference when the external reference is not available. Note that when using a mapping, the local copy is always used, and the external reference is not queried.
- Loading branch information
Showing
6 changed files
with
200 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/resources/com/networknt/schema/url-mapping.schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"title": "json-schema-validator-url-mapping", | ||
"type": "array", | ||
"description": "Data portion of message from JMRI to client for type \"node\"", | ||
"items": { | ||
"type": "object", | ||
"uniqueItems": true, | ||
"properties": { | ||
"publicURL": { | ||
"type": "string", | ||
"description": "Public, presumably internet-accessible, URL for schema" | ||
}, | ||
"localURL": { | ||
"type": "string", | ||
"description": "Local URL for schema that will be used when a schema references the public URL" | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"required": [ | ||
"publicURL", | ||
"localURL" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.networknt.schema; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.net.UnknownHostException; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.networknt.schema.url.URLFactory; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
public class UrlMappingTest { | ||
|
||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
/** | ||
* Validate that a JSON URL Mapping file containing the URL Mapping schema | ||
* is schema valid. | ||
* | ||
* @throws IOException if unable to parse the mapping file | ||
*/ | ||
@Test | ||
public void testUrlMappingUrl() throws IOException { | ||
JsonSchemaFactory instance = JsonSchemaFactory.getInstance(); | ||
URL mappings = URLFactory.toURL("resource:tests/url_mapping/url-mapping.json"); | ||
instance.addUrlMappings(mappings); | ||
JsonSchema schema = instance.getSchema(new URL("https://raw.githubusercontent.com/networknt/json-schema-validator/master/src/main/resources/url-mapping.schema.json")); | ||
assertEquals(0, schema.validate(mapper.readTree(mappings)).size()); | ||
} | ||
|
||
/** | ||
* Validate that local URL is used when attempting to get a schema that is not | ||
* available publicly. Use the URL http://example.com/invalid/schema/url to use | ||
* a public URL that returns a 404 Not Found. The locally mapped schema is a | ||
* valid, but empty schema. | ||
* | ||
* @throws IOException if unable to parse the mapping file | ||
*/ | ||
@Test | ||
public void testExampleMappings() throws IOException { | ||
JsonSchemaFactory instance = JsonSchemaFactory.getInstance(); | ||
URL example = new URL("http://example.com/invalid/schema/url"); | ||
// first test that attempting to use example URL throws an error | ||
try { | ||
JsonSchema schema = instance.getSchema(example); | ||
schema.validate(mapper.createObjectNode()); | ||
fail("Expected exception not thrown"); | ||
} catch (JsonSchemaException ex) { | ||
Throwable cause = ex.getCause(); | ||
if (!(cause instanceof FileNotFoundException || | ||
cause instanceof UnknownHostException)) { | ||
fail("Unexpected cause for JsonSchemaException"); | ||
} | ||
// passing, so do nothing | ||
} catch (Exception ex) { | ||
fail("Unexpected exception thrown"); | ||
} | ||
URL mappings = URLFactory.toURL("resource:tests/url_mapping/invalid-schema-url.json"); | ||
instance.addUrlMappings(mappings); | ||
JsonSchema schema = instance.getSchema(example); | ||
assertEquals(0, schema.validate(mapper.createObjectNode()).size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#" | ||
} |
10 changes: 10 additions & 0 deletions
10
src/test/resources/tests/url_mapping/invalid-schema-url.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"publicURL": "http://json-schema.org/draft-04/schema#", | ||
"localURL": "resource:/draftv4.schema.json" | ||
}, | ||
{ | ||
"publicURL": "http://example.com/invalid/schema/url", | ||
"localURL": "resource:/tests/url_mapping/example-schema.json" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"publicURL": "http://json-schema.org/draft-04/schema#", | ||
"localURL": "resource:/draftv4.schema.json" | ||
}, | ||
{ | ||
"publicURL": "https://raw.githubusercontent.com/networknt/json-schema-validator/master/src/main/resources/url-mapping.schema.json", | ||
"localURL": "resource:/com/networknt/schema/url-mapping.schema.json" | ||
} | ||
] |