Skip to content

Commit b1aec99

Browse files
authored
Add ability to detect spec version optionally (#646)
1 parent 5645d56 commit b1aec99

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

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

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,69 @@
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
2020

21+
import java.util.Optional;
22+
2123
/**
2224
* This class is used to detect schema version
2325
*
2426
* @author Subhajitdas298
2527
* @since 25/06/20
2628
*/
27-
public class SpecVersionDetector {
29+
public final class SpecVersionDetector {
2830

2931
private static final String SCHEMA_TAG = "$schema";
3032

33+
private SpecVersionDetector() {
34+
// Prevent instantiation of this utility class
35+
}
36+
3137
/**
32-
* Detects schema version based on the schema tag
38+
* Detects schema version based on the schema tag: if the schema tag is not present, throws
39+
* {@link JsonSchemaException} with the corresponding message, otherwise - returns the detected spec version.
3340
*
34-
* @param jsonNode Json Node to read from
35-
* @return Spec version
41+
* @param jsonNode JSON Node to read from
42+
* @return Spec version if present, otherwise throws an exception
3643
*/
3744
public static SpecVersion.VersionFlag detect(JsonNode jsonNode) {
38-
JsonNode schemaTag = jsonNode.get(SCHEMA_TAG);
39-
if (schemaTag == null) {
40-
throw new JsonSchemaException("'" + SCHEMA_TAG + "' tag is not present");
41-
}
45+
return detectOptionalVersion(jsonNode).orElseThrow(
46+
() -> new JsonSchemaException("'" + SCHEMA_TAG + "' tag is not present")
47+
);
48+
}
49+
50+
/**
51+
* Detects schema version based on the schema tag: if the schema tag is not present, returns an empty {@link
52+
* Optional} value, otherwise - returns the detected spec version wrapped into {@link Optional}.
53+
*
54+
* @param jsonNode JSON Node to read from
55+
* @return Spec version if present, otherwise empty
56+
*/
57+
public static Optional<SpecVersion.VersionFlag> detectOptionalVersion(JsonNode jsonNode) {
58+
return Optional.ofNullable(jsonNode.get(SCHEMA_TAG)).map(schemaTag -> {
59+
60+
final boolean forceHttps = true;
61+
final boolean removeEmptyFragmentSuffix = true;
4262

43-
final boolean forceHttps = true;
44-
final boolean removeEmptyFragmentSuffix = true;
63+
String schemaTagValue = schemaTag.asText();
64+
String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(schemaTagValue, forceHttps,
65+
removeEmptyFragmentSuffix);
4566

46-
String schemaTagValue = schemaTag.asText();
47-
String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(schemaTagValue, forceHttps, removeEmptyFragmentSuffix);
48-
if (schemaUri.equals(JsonMetaSchema.getV4().getUri())) {
49-
return SpecVersion.VersionFlag.V4;
50-
}
51-
if (schemaUri.equals(JsonMetaSchema.getV6().getUri())) {
52-
return SpecVersion.VersionFlag.V6;
53-
}
54-
if (schemaUri.equals(JsonMetaSchema.getV7().getUri())) {
55-
return SpecVersion.VersionFlag.V7;
56-
}
57-
if (schemaUri.equals(JsonMetaSchema.getV201909().getUri())) {
58-
return SpecVersion.VersionFlag.V201909;
59-
}
60-
if (schemaUri.equals(JsonMetaSchema.getV202012().getUri())) {
61-
return SpecVersion.VersionFlag.V202012;
62-
}
63-
throw new JsonSchemaException("'" + schemaTagValue + "' is unrecognizable schema");
67+
if (schemaUri.equals(JsonMetaSchema.getV4().getUri())) {
68+
return SpecVersion.VersionFlag.V4;
69+
}
70+
if (schemaUri.equals(JsonMetaSchema.getV6().getUri())) {
71+
return SpecVersion.VersionFlag.V6;
72+
}
73+
if (schemaUri.equals(JsonMetaSchema.getV7().getUri())) {
74+
return SpecVersion.VersionFlag.V7;
75+
}
76+
if (schemaUri.equals(JsonMetaSchema.getV201909().getUri())) {
77+
return SpecVersion.VersionFlag.V201909;
78+
}
79+
if (schemaUri.equals(JsonMetaSchema.getV202012().getUri())) {
80+
return SpecVersion.VersionFlag.V202012;
81+
}
82+
throw new JsonSchemaException("'" + schemaTagValue + "' is unrecognizable schema");
83+
});
6484
}
6585

6686
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

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

89
import java.io.IOException;
910
import java.io.InputStream;
11+
import java.util.Optional;
1012

1113
import static org.junit.jupiter.api.Assertions.assertEquals;
1214
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -42,4 +44,13 @@ void detectInvalidSchemaVersion(String schemaPath, String expectedError) throws
4244
JsonSchemaException exception = assertThrows(JsonSchemaException.class, () -> SpecVersionDetector.detect(node));
4345
assertEquals(expectedError, exception.getMessage());
4446
}
45-
}
47+
48+
@Test
49+
void detectOptionalSpecVersion() throws IOException {
50+
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(
51+
"data/schemaTagMissing.json");
52+
JsonNode node = mapper.readTree(in);
53+
Optional<SpecVersion.VersionFlag> flag = SpecVersionDetector.detectOptionalVersion(node);
54+
assertEquals(Optional.empty(), flag);
55+
}
56+
}

0 commit comments

Comments
 (0)