Skip to content

Commit 9e2e151

Browse files
authored
Improve error messages on spec version detection (#641)
1 parent 816c167 commit 9e2e151

File tree

2 files changed

+40
-73
lines changed

2 files changed

+40
-73
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
*/
2727
public class SpecVersionDetector {
2828

29-
// Schema tag
3029
private static final String SCHEMA_TAG = "$schema";
3130

3231
/**
@@ -36,25 +35,32 @@ public class SpecVersionDetector {
3635
* @return Spec version
3736
*/
3837
public static SpecVersion.VersionFlag detect(JsonNode jsonNode) {
39-
if (!jsonNode.has(SCHEMA_TAG))
40-
throw new JsonSchemaException("Schema tag not present");
38+
JsonNode schemaTag = jsonNode.get(SCHEMA_TAG);
39+
if (schemaTag == null) {
40+
throw new JsonSchemaException("'" + SCHEMA_TAG + "' tag is not present");
41+
}
4142

4243
final boolean forceHttps = true;
4344
final boolean removeEmptyFragmentSuffix = true;
4445

45-
String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(jsonNode.get(SCHEMA_TAG).asText(), forceHttps, removeEmptyFragmentSuffix);
46-
if (schemaUri.equals(JsonMetaSchema.getV4().getUri()))
46+
String schemaTagValue = schemaTag.asText();
47+
String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(schemaTagValue, forceHttps, removeEmptyFragmentSuffix);
48+
if (schemaUri.equals(JsonMetaSchema.getV4().getUri())) {
4749
return SpecVersion.VersionFlag.V4;
48-
else if (schemaUri.equals(JsonMetaSchema.getV6().getUri()))
50+
}
51+
if (schemaUri.equals(JsonMetaSchema.getV6().getUri())) {
4952
return SpecVersion.VersionFlag.V6;
50-
else if (schemaUri.equals(JsonMetaSchema.getV7().getUri()))
53+
}
54+
if (schemaUri.equals(JsonMetaSchema.getV7().getUri())) {
5155
return SpecVersion.VersionFlag.V7;
52-
else if (schemaUri.equals(JsonMetaSchema.getV201909().getUri()))
56+
}
57+
if (schemaUri.equals(JsonMetaSchema.getV201909().getUri())) {
5358
return SpecVersion.VersionFlag.V201909;
54-
else if (schemaUri.equals(JsonMetaSchema.getV202012().getUri()))
59+
}
60+
if (schemaUri.equals(JsonMetaSchema.getV202012().getUri())) {
5561
return SpecVersion.VersionFlag.V202012;
56-
else
57-
throw new JsonSchemaException("Unrecognizable schema");
62+
}
63+
throw new JsonSchemaException("'" + schemaTagValue + "' is unrecognizable schema");
5864
}
5965

6066
}

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

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,44 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
import java.io.IOException;
89
import java.io.InputStream;
910

1011
import static org.junit.jupiter.api.Assertions.assertEquals;
1112
import static org.junit.jupiter.api.Assertions.assertThrows;
1213

13-
public class SpecVersionDetectorTest {
14-
15-
private static final String SCHEMA_TAG_JSON = "schemaTag.json";
14+
class SpecVersionDetectorTest {
1615

1716
private static final ObjectMapper mapper = new ObjectMapper();
1817

19-
@Test
20-
public void detectV4() throws IOException {
21-
InputStream in = Thread.currentThread().getContextClassLoader()
22-
.getResourceAsStream("draft4/" + SCHEMA_TAG_JSON);
23-
JsonNode node = mapper.readTree(in);
24-
SpecVersion.VersionFlag flag = SpecVersionDetector.detect(node);
25-
assertEquals(SpecVersion.VersionFlag.V4, flag);
26-
}
27-
28-
@Test
29-
public void detectV6() throws IOException {
18+
@ParameterizedTest
19+
@CsvSource({
20+
"draft4, V4",
21+
"draft6, V6",
22+
"draft7, V7",
23+
"draft2019-09, V201909",
24+
"draft2020-12, V202012"
25+
})
26+
void detectVersion(String resourceDirectory, SpecVersion.VersionFlag expectedFlag) throws IOException {
3027
InputStream in = Thread.currentThread().getContextClassLoader()
31-
.getResourceAsStream("draft6/" + SCHEMA_TAG_JSON);
28+
.getResourceAsStream(resourceDirectory + "/schemaTag.json");
3229
JsonNode node = mapper.readTree(in);
3330
SpecVersion.VersionFlag flag = SpecVersionDetector.detect(node);
34-
assertEquals(SpecVersion.VersionFlag.V6, flag);
31+
assertEquals(expectedFlag, flag);
3532
}
3633

37-
@Test
38-
public void detectV7() throws IOException {
39-
InputStream in = Thread.currentThread().getContextClassLoader()
40-
.getResourceAsStream("draft7/" + SCHEMA_TAG_JSON);
34+
@ParameterizedTest
35+
@CsvSource({
36+
"data/schemaTag.json, 'http://json-schema.org/draft-03/schema#' is unrecognizable schema",
37+
"data/schemaTagMissing.json, '$schema' tag is not present"
38+
})
39+
void detectInvalidSchemaVersion(String schemaPath, String expectedError) throws IOException {
40+
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaPath);
4141
JsonNode node = mapper.readTree(in);
42-
SpecVersion.VersionFlag flag = SpecVersionDetector.detect(node);
43-
assertEquals(SpecVersion.VersionFlag.V7, flag);
42+
JsonSchemaException exception = assertThrows(JsonSchemaException.class, () -> SpecVersionDetector.detect(node));
43+
assertEquals(expectedError, exception.getMessage());
4444
}
45-
46-
@Test
47-
public void detectV201909() throws IOException {
48-
InputStream in = Thread.currentThread().getContextClassLoader()
49-
.getResourceAsStream("draft2019-09/" + SCHEMA_TAG_JSON);
50-
JsonNode node = mapper.readTree(in);
51-
SpecVersion.VersionFlag flag = SpecVersionDetector.detect(node);
52-
assertEquals(SpecVersion.VersionFlag.V201909, flag);
53-
}
54-
55-
@Test
56-
public void detectV202012() throws IOException {
57-
InputStream in = Thread.currentThread().getContextClassLoader()
58-
.getResourceAsStream("draft2020-12/" + SCHEMA_TAG_JSON);
59-
JsonNode node = mapper.readTree(in);
60-
SpecVersion.VersionFlag flag = SpecVersionDetector.detect(node);
61-
assertEquals(SpecVersion.VersionFlag.V202012, flag);
62-
}
63-
64-
@Test
65-
public void detectUnsupportedSchemaVersion() {
66-
assertThrows(JsonSchemaException.class, () -> {
67-
InputStream in = Thread.currentThread().getContextClassLoader()
68-
.getResourceAsStream("data/" + SCHEMA_TAG_JSON);
69-
JsonNode node = mapper.readTree(in);
70-
SpecVersion.VersionFlag flag = SpecVersionDetector.detect(node);
71-
});
72-
}
73-
74-
@Test
75-
public void detectMissingSchemaVersion() {
76-
assertThrows(JsonSchemaException.class, () -> {
77-
InputStream in = Thread.currentThread().getContextClassLoader()
78-
.getResourceAsStream("data/" + "schemaTagMissing.json");
79-
JsonNode node = mapper.readTree(in);
80-
SpecVersion.VersionFlag flag = SpecVersionDetector.detect(node);
81-
});
82-
}
83-
8445
}

0 commit comments

Comments
 (0)