Skip to content

Commit ba7e96a

Browse files
committed
test: add test that invalid external ref fails if not mapped
1 parent 706e60d commit ba7e96a

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

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(mappings)).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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "json-object-with-schema",
4+
"type": "object",
5+
"description": "A JSON object and the schema to evaluate it against",
6+
"properties": {
7+
"for": {
8+
"type": "object",
9+
"description": "The object to evaluate"
10+
},
11+
"schema": {
12+
"$ref": "http://example.com/invalid/schema/url",
13+
"description": "The Schema to evaluate the object against (using invalid URL to force failure if mapping is not correct)"
14+
}
15+
},
16+
"additionalProperties": false,
17+
"required": [
18+
"for",
19+
"schema"
20+
]
21+
}

0 commit comments

Comments
 (0)