Skip to content

Commit d615f95

Browse files
authored
Merge pull request #135 from rhwood/url-mapping-refs
feat: Use URL mappings for $ref and other URLs
2 parents d2a3a8d + 350013b commit d615f95

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static JsonSchema obtainSubSchemaNode(JsonNode schemaNode, ValidationCon
8383
}
8484
else {
8585
URL url = URLFactory.toURL(node.textValue());
86-
return validationContext.getJsonSchemaFactory().getSchema(url);
86+
return validationContext.getJsonSchemaFactory().getSchema(url, validationContext.getConfig());
8787
}
8888
} catch (MalformedURLException e) {
8989
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static JsonSchema getRefSchema(JsonSchema parentSchema, ValidationContext valida
6262

6363
try {
6464
URL url = URLFactory.toURL(schemaUrl);
65-
parentSchema = validationContext.getJsonSchemaFactory().getSchema(url);
65+
parentSchema = validationContext.getJsonSchemaFactory().getSchema(url, validationContext.getConfig());
6666
} catch (MalformedURLException e) {
6767
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaUrl);
6868
parentSchema = validationContext.getJsonSchemaFactory().getSchema(is);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ public void testValidatorConfigExampleMappings() throws IOException {
130130
assertEquals(0, schema.validate(mapper.createObjectNode()).size());
131131
}
132132

133+
@Test
134+
public void testMappingsForRef() throws IOException {
135+
JsonSchemaFactory instance = JsonSchemaFactory.getInstance();
136+
URL mappings = URLFactory.toURL("resource:tests/url_mapping/schema-with-ref-mapping.json");
137+
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
138+
config.setUrlMappings(getUrlMappingsFromUrl(mappings));
139+
JsonSchema schema = instance.getSchema(URLFactory.toURL("resource:tests/url_mapping/schema-with-ref.json"),
140+
config);
141+
assertEquals(0, schema.validate(mapper.readTree("[]")).size());
142+
}
143+
133144
private Map<URL, URL> getUrlMappingsFromUrl(URL url) throws MalformedURLException, IOException {
134145
HashMap<URL, URL> map = new HashMap<URL, URL>();
135146
for (JsonNode mapping : mapper.readTree(url)) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"publicURL": "http://json-schema.org/draft-04/schema#",
4+
"localURL": "resource:/draftv4.schema.json"
5+
},
6+
{
7+
"publicURL": "http://example.com/invalid/schema/url",
8+
"localURL": "resource:/tests/url_mapping/example-schema.json"
9+
}
10+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "json-object-with-schema",
4+
"type": "array",
5+
"properties": {
6+
"schema": {
7+
"$ref": "http://example.com/invalid/schema/url"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)