Skip to content

Commit 708bec8

Browse files
author
Jake Waffle
committed
Fixed some issues with the previous commit and made it so that schemas have to be valid for the unit tests.
1 parent f7d767a commit 708bec8

File tree

6 files changed

+72
-112
lines changed

6 files changed

+72
-112
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ private JsonSchema(ValidationContext validationContext, String schemaPath, URL
7373
super(schemaPath, schemaNode, parent, null, suppressSubSchemaRetrieval);
7474
this.validationContext = validationContext;
7575
this.config = validationContext.getConfig();
76-
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
7776
this.currentUrl = this.combineCurrentUrlWithIds(currentUrl, schemaNode);
77+
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
7878
}
7979

8080
private URL combineCurrentUrlWithIds(URL currentUrl, JsonNode schemaNode) {

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

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public static class Builder {
4242
private String defaultMetaSchemaURI;
4343
private Map<String, JsonMetaSchema> jsonMetaSchemas = new HashMap<String, JsonMetaSchema>();
4444
private Map<URL, URL> urlMap = new HashMap<URL, URL>();
45-
private URL rootSchemaUrl = null;
4645

4746
public Builder objectMapper(ObjectMapper objectMapper) {
4847
this.objectMapper = objectMapper;
@@ -76,21 +75,14 @@ public Builder addUrlMappings(Map<URL, URL> map) {
7675
return this;
7776
}
7877

79-
public Builder setRootSchemaUrl(URL rootSchemaUrl)
80-
{
81-
this.rootSchemaUrl = rootSchemaUrl;
82-
return this;
83-
}
84-
8578
public JsonSchemaFactory build() {
8679
// create builtin keywords with (custom) formats.
8780
return new JsonSchemaFactory(
8881
objectMapper == null ? new ObjectMapper() : objectMapper,
8982
urlFetcher == null ? new StandardURLFetcher(): urlFetcher,
9083
defaultMetaSchemaURI,
9184
jsonMetaSchemas,
92-
urlMap,
93-
rootSchemaUrl
85+
urlMap
9486
);
9587
}
9688
}
@@ -100,14 +92,12 @@ public JsonSchemaFactory build() {
10092
private final String defaultMetaSchemaURI;
10193
private final Map<String, JsonMetaSchema> jsonMetaSchemas;
10294
private final Map<URL, URL> urlMap;
103-
private final URL rootSchemaUrl;
10495

10596
private JsonSchemaFactory(
10697
ObjectMapper mapper,
10798
URLFetcher urlFetcher,
10899
String defaultMetaSchemaURI,
109-
Map<String, JsonMetaSchema> jsonMetaSchemas, Map<URL, URL> urlMap,
110-
URL rootSchemaUrl) {
100+
Map<String, JsonMetaSchema> jsonMetaSchemas, Map<URL, URL> urlMap) {
111101
if (mapper == null) {
112102
throw new IllegalArgumentException("ObjectMapper must not be null");
113103
}
@@ -131,7 +121,6 @@ private JsonSchemaFactory(
131121
this.urlFetcher = urlFetcher;
132122
this.jsonMetaSchemas = jsonMetaSchemas;
133123
this.urlMap = urlMap;
134-
this.rootSchemaUrl = rootSchemaUrl;
135124
}
136125

137126
/**
@@ -163,14 +152,13 @@ public static Builder builder(JsonSchemaFactory blueprint) {
163152
.urlFetcher(blueprint.urlFetcher)
164153
.defaultMetaSchemaURI(blueprint.defaultMetaSchemaURI)
165154
.objectMapper(blueprint.mapper)
166-
.addUrlMappings(blueprint.urlMap)
167-
.setRootSchemaUrl(blueprint.rootSchemaUrl);
155+
.addUrlMappings(blueprint.urlMap);
168156
}
169157

170-
private JsonSchema newJsonSchema(JsonNode schemaNode, SchemaValidatorsConfig config) {
158+
private JsonSchema newJsonSchema(URL schemaUrl, JsonNode schemaNode, SchemaValidatorsConfig config) {
171159
final ValidationContext validationContext = createValidationContext(schemaNode);
172160
validationContext.setConfig(config);
173-
JsonSchema jsonSchema = new JsonSchema(validationContext, this.rootSchemaUrl, schemaNode);
161+
JsonSchema jsonSchema = new JsonSchema(validationContext, schemaUrl, schemaNode);
174162
return jsonSchema;
175163
}
176164

@@ -188,33 +176,61 @@ private JsonMetaSchema findMetaSchemaForSchema(JsonNode schemaNode) {
188176
}
189177
return jsonMetaSchema;
190178
}
191-
179+
192180
public JsonSchema getSchema(String schema, SchemaValidatorsConfig config) {
193181
try {
194182
final JsonNode schemaNode = mapper.readTree(schema);
195-
return newJsonSchema(schemaNode, config);
183+
return newJsonSchema(null, schemaNode, config);
184+
} catch (IOException ioe) {
185+
logger.error("Failed to load json schema!", ioe);
186+
throw new JsonSchemaException(ioe);
187+
}
188+
}
189+
190+
public JsonSchema getSchema(URL schemaUrl, String schema, SchemaValidatorsConfig config) {
191+
try {
192+
final JsonNode schemaNode = mapper.readTree(schema);
193+
return newJsonSchema(schemaUrl, schemaNode, config);
196194
} catch (IOException ioe) {
197195
logger.error("Failed to load json schema!", ioe);
198196
throw new JsonSchemaException(ioe);
199197
}
200198
}
201199

202200
public JsonSchema getSchema(String schema) {
203-
return getSchema(schema, null);
201+
return getSchema(null, schema, null);
202+
}
203+
204+
public JsonSchema getSchema(URL schemaUrl, String schema) {
205+
return getSchema(schemaUrl, schema, null);
204206
}
205207

206208
public JsonSchema getSchema(InputStream schemaStream, SchemaValidatorsConfig config) {
207209
try {
208210
final JsonNode schemaNode = mapper.readTree(schemaStream);
209-
return newJsonSchema(schemaNode, config);
211+
return newJsonSchema(null, schemaNode, config);
212+
} catch (IOException ioe) {
213+
logger.error("Failed to load json schema!", ioe);
214+
throw new JsonSchemaException(ioe);
215+
}
216+
}
217+
218+
public JsonSchema getSchema(URL schemaUrl, InputStream schemaStream, SchemaValidatorsConfig config) {
219+
try {
220+
final JsonNode schemaNode = mapper.readTree(schemaStream);
221+
return newJsonSchema(schemaUrl, schemaNode, config);
210222
} catch (IOException ioe) {
211223
logger.error("Failed to load json schema!", ioe);
212224
throw new JsonSchemaException(ioe);
213225
}
214226
}
215227

216228
public JsonSchema getSchema(InputStream schemaStream) {
217-
return getSchema(schemaStream, null);
229+
return getSchema(null, schemaStream, null);
230+
}
231+
232+
public JsonSchema getSchema(URL schemaUrl, InputStream schemaStream) {
233+
return getSchema(schemaUrl, schemaStream, null);
218234
}
219235

220236
public JsonSchema getSchema(URL schemaURL, SchemaValidatorsConfig config) {
@@ -232,7 +248,7 @@ public JsonSchema getSchema(URL schemaURL, SchemaValidatorsConfig config) {
232248
return new JsonSchema(new ValidationContext(jsonMetaSchema, this), mappedURL, schemaNode, true /*retrieved via id, resolving will not change anything*/);
233249
}
234250

235-
return newJsonSchema(schemaNode, config);
251+
return newJsonSchema(mappedURL, schemaNode, config);
236252
} finally {
237253
if (inputStream != null) {
238254
inputStream.close();
@@ -245,15 +261,23 @@ public JsonSchema getSchema(URL schemaURL, SchemaValidatorsConfig config) {
245261
}
246262

247263
public JsonSchema getSchema(URL schemaURL) {
248-
return getSchema(schemaURL, null);
264+
return getSchema(schemaURL, new SchemaValidatorsConfig());
249265
}
250266

251267
public JsonSchema getSchema(JsonNode jsonNode, SchemaValidatorsConfig config) {
252-
return newJsonSchema(jsonNode, config);
268+
return newJsonSchema(null, jsonNode, config);
253269
}
254-
270+
255271
public JsonSchema getSchema(JsonNode jsonNode) {
256-
return newJsonSchema(jsonNode, null);
272+
return newJsonSchema(null, jsonNode, null);
273+
}
274+
275+
public JsonSchema getSchema(URL schemaUrl, JsonNode jsonNode, SchemaValidatorsConfig config) {
276+
return newJsonSchema(schemaUrl, jsonNode, config);
277+
}
278+
279+
public JsonSchema getSchema(URL schemaUrl, JsonNode jsonNode) {
280+
return newJsonSchema(schemaUrl, jsonNode, null);
257281
}
258282

259283
private boolean idMatchesSourceUrl(JsonMetaSchema metaSchema, JsonNode schema, URL schemaUrl) {

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,26 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.fasterxml.jackson.databind.ObjectMapper;
21-
import com.fasterxml.jackson.databind.node.ArrayNode;
22-
import io.undertow.Undertow;
23-
import io.undertow.server.handlers.resource.FileResourceManager;
24-
import org.junit.AfterClass;
25-
import org.junit.Assert;
26-
import org.junit.BeforeClass;
27-
import org.junit.Test;
19+
import static io.undertow.Handlers.resource;
2820

2921
import java.io.File;
3022
import java.io.InputStream;
3123
import java.net.URL;
3224
import java.util.ArrayList;
3325
import java.util.List;
3426

35-
import static io.undertow.Handlers.resource;
27+
import org.junit.AfterClass;
28+
import org.junit.Assert;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
import com.fasterxml.jackson.databind.JsonNode;
33+
import com.fasterxml.jackson.databind.ObjectMapper;
34+
import com.fasterxml.jackson.databind.node.ArrayNode;
35+
import com.networknt.schema.url.URLFactory;
36+
37+
import io.undertow.Undertow;
38+
import io.undertow.server.handlers.resource.FileResourceManager;
3639

3740
public class JsonSchemaTest {
3841
protected ObjectMapper mapper = new ObjectMapper();
@@ -67,6 +70,7 @@ public static void tearDown() throws Exception {
6770
}
6871

6972
private void runTestFile(String testCaseFile) throws Exception {
73+
final URL testCaseFileUrl = URLFactory.toURL("classpath:" + testCaseFile);
7074
InputStream in = Thread.currentThread().getContextClassLoader()
7175
.getResourceAsStream(testCaseFile);
7276
ArrayNode testCases = (ArrayNode) mapper.readTree(in);
@@ -84,7 +88,7 @@ private void runTestFile(String testCaseFile) throws Exception {
8488
// Configure the schemaValidator to set typeLoose's value based on the test file,
8589
// if test file do not contains typeLoose flag, use default value: true.
8690
config.setTypeLoose((typeLooseNode == null) ? true : typeLooseNode.asBoolean());
87-
JsonSchema schema = validatorFactory.getSchema(testCase.get("schema"), config);
91+
JsonSchema schema = validatorFactory.getSchema(testCaseFileUrl, testCase.get("schema"), config);
8892
List<ValidationMessage> errors = new ArrayList<ValidationMessage>();
8993

9094
errors.addAll(schema.validate(node));
@@ -106,7 +110,7 @@ private void runTestFile(String testCaseFile) throws Exception {
106110
}
107111
}
108112
} catch (JsonSchemaException e) {
109-
System.out.println("Bypass validation due to invalid schema: " + e.getMessage());
113+
throw new IllegalStateException(String.format("Current schema should not be invalid: %s", testCaseFile), e);
110114
}
111115
}
112116
}

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

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "integer"
3+
}

src/test/resources/tests/relativeRefRemote.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
{
3535
"description": "ref within remote ref",
3636
"schema": {
37-
"$ref": "refRemoveSchema.json"
37+
"$ref": "refRemoteSchema.json"
3838
},
3939
"tests": [
4040
{

0 commit comments

Comments
 (0)