Skip to content

Commit 52e606a

Browse files
Added schema version detection method
1 parent 2fa85e6 commit 52e606a

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,51 +27,49 @@
2727
* Created by steve on 22/10/16.
2828
*/
2929
public class BaseJsonSchemaValidatorTest {
30+
31+
private ObjectMapper mapper = new ObjectMapper();
32+
3033
protected JsonNode getJsonNodeFromClasspath(String name) throws Exception {
3134
InputStream is1 = Thread.currentThread().getContextClassLoader()
3235
.getResourceAsStream(name);
33-
34-
ObjectMapper mapper = new ObjectMapper();
35-
JsonNode node = mapper.readTree(is1);
36-
return node;
36+
return mapper.readTree(is1);
3737
}
3838

3939
protected JsonNode getJsonNodeFromStringContent(String content) throws Exception {
40-
ObjectMapper mapper = new ObjectMapper();
41-
JsonNode node = mapper.readTree(content);
42-
return node;
40+
return mapper.readTree(content);
4341
}
4442

4543
protected JsonNode getJsonNodeFromUrl(String url) throws Exception {
46-
ObjectMapper mapper = new ObjectMapper();
47-
JsonNode node = mapper.readTree(new URL(url));
48-
return node;
44+
return mapper.readTree(new URL(url));
4945
}
5046

51-
protected JsonSchema getJsonSchemaFromClasspath(String name) throws Exception {
47+
protected JsonSchema getJsonSchemaFromClasspath(String name) {
5248
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
5349
InputStream is = Thread.currentThread().getContextClassLoader()
5450
.getResourceAsStream(name);
55-
JsonSchema schema = factory.getSchema(is);
56-
return schema;
51+
return factory.getSchema(is);
5752
}
5853

59-
60-
protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) throws Exception {
54+
protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
6155
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
62-
JsonSchema schema = factory.getSchema(schemaContent);
63-
return schema;
56+
return factory.getSchema(schemaContent);
6457
}
6558

6659
protected JsonSchema getJsonSchemaFromUrl(String uri) throws Exception {
6760
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
68-
JsonSchema schema = factory.getSchema(new URI(uri));
69-
return schema;
61+
return factory.getSchema(new URI(uri));
7062
}
7163

72-
protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) throws Exception {
64+
protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) {
7365
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
74-
JsonSchema schema = factory.getSchema(jsonNode);
75-
return schema;
66+
return factory.getSchema(jsonNode);
7667
}
68+
69+
// Automatically detect version for given JsonNode
70+
protected JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
71+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNode));
72+
return factory.getSchema(jsonNode);
73+
}
74+
7775
}

0 commit comments

Comments
 (0)