Skip to content

Commit bfa46d7

Browse files
committed
resolving #217 by changing spec version deduction mechanism so that it considers explicitly set spec version if the value of $schema cannot be resolved to any spec versions
1 parent 956a455 commit bfa46d7

File tree

4 files changed

+93
-6
lines changed

4 files changed

+93
-6
lines changed

core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public static class SchemaLoaderBuilder {
6262

6363
SpecificationVersion specVersion;
6464

65+
private boolean specVersionIsExplicitlySet = false;
66+
6567
boolean useDefaults = false;
6668

6769
private boolean nullableSupport = false;
@@ -105,11 +107,13 @@ public SchemaLoaderBuilder addFormatValidator(String formatName,
105107

106108
public SchemaLoaderBuilder draftV6Support() {
107109
setSpecVersion(DRAFT_6);
110+
specVersionIsExplicitlySet = true;
108111
return this;
109112
}
110113

111114
public SchemaLoaderBuilder draftV7Support() {
112115
setSpecVersion(DRAFT_7);
116+
specVersionIsExplicitlySet = true;
113117
return this;
114118
}
115119

@@ -125,7 +129,7 @@ private Optional<SpecificationVersion> specVersionInSchema() {
125129
try {
126130
specVersion = Optional.ofNullable(metaSchemaURL).map((SpecificationVersion::getByMetaSchemaUrl));
127131
} catch (IllegalArgumentException e) {
128-
throw new SchemaException("#", e.getMessage());
132+
return specVersion;
129133
}
130134
}
131135
return specVersion;
@@ -275,9 +279,21 @@ public SchemaLoader(SchemaLoaderBuilder builder) {
275279
Object effectiveRootSchemaJson = builder.rootSchemaJson == null
276280
? builder.schemaJson
277281
: builder.rootSchemaJson;
278-
SpecificationVersion specVersion = extractSchemaKeywordValue(effectiveRootSchemaJson)
279-
.map(SpecificationVersion::getByMetaSchemaUrl)
280-
.orElse(builder.specVersion);
282+
Optional<String> schemaKeywordValue = extractSchemaKeywordValue(effectiveRootSchemaJson);
283+
SpecificationVersion specVersion;
284+
if (schemaKeywordValue.isPresent()) {
285+
try {
286+
specVersion = SpecificationVersion.getByMetaSchemaUrl(schemaKeywordValue.get());
287+
} catch (IllegalArgumentException e) {
288+
if (builder.specVersionIsExplicitlySet) {
289+
specVersion = builder.specVersion;
290+
} else {
291+
throw new SchemaException("#", "could not determine version");
292+
}
293+
}
294+
} else {
295+
specVersion = builder.specVersion;
296+
}
281297
this.config = new LoaderConfig(builder.httpClient,
282298
builder.formatValidators,
283299
specVersion,

core/src/main/java/org/everit/json/schema/loader/SpecificationVersion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private static List<String> keywords(String... keywords) {
155155

156156
private static Map<String, FormatValidator> formatValidators(Map<String, FormatValidator> parent, FormatValidator... validators) {
157157
Map<String, FormatValidator> validatorMap = (parent == null) ? new HashMap<>() : new HashMap<>(parent);
158-
for(FormatValidator validator : validators) {
158+
for (FormatValidator validator : validators) {
159159
validatorMap.put(validator.formatName(), validator);
160160
}
161161
return unmodifiableMap(validatorMap);

core/src/test/java/org/everit/json/schema/loader/SchemaLoaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class SchemaLoaderTest {
6161

6262
private static JSONObject ALL_SCHEMAS = ResourceLoader.DEFAULT.readObj("testschemas.json");
6363

64-
private static JSONObject get(final String schemaName) {
64+
private static JSONObject get(String schemaName) {
6565
return ALL_SCHEMAS.getJSONObject(schemaName);
6666
}
6767

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.everit.json.schema.loader;
2+
3+
import static org.everit.json.schema.loader.SpecificationVersion.DRAFT_4;
4+
import static org.everit.json.schema.loader.SpecificationVersion.DRAFT_6;
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertSame;
7+
import static org.junit.Assert.fail;
8+
9+
import org.everit.json.schema.ResourceLoader;
10+
import org.everit.json.schema.SchemaException;
11+
import org.json.JSONObject;
12+
import org.junit.Test;
13+
14+
public class SpecVersionDeductionTest {
15+
16+
private static JSONObject ALL_SCHEMAS = ResourceLoader.DEFAULT.readObj("testschemas.json");
17+
18+
private static JSONObject get(String schemaName) {
19+
return ALL_SCHEMAS.getJSONObject(schemaName);
20+
}
21+
22+
private void assertSchemaException(SchemaLoader.SchemaLoaderBuilder loaderBuilder) {
23+
try {
24+
loaderBuilder.build();
25+
fail("did not throw exception");
26+
} catch (SchemaException e) {
27+
assertEquals("#", e.getSchemaLocation());
28+
}
29+
}
30+
31+
private void assertSpecVersion(SpecificationVersion version, SchemaLoader.SchemaLoaderBuilder loaderBuilder) {
32+
assertSame(version, loaderBuilder.build().specVersion());
33+
}
34+
35+
@Test
36+
public void unknownMetaSchema_implicitDefault() {
37+
assertSchemaException(SchemaLoader.builder().schemaJson(get("unknownMetaSchema")));
38+
}
39+
40+
@Test
41+
public void knownMetaSchema_implicitDefault() {
42+
assertSpecVersion(DRAFT_6, SchemaLoader.builder().schemaJson(get("explicitSchemaVersion")));
43+
}
44+
45+
@Test
46+
public void missingMetaSchema_implicitDefault() {
47+
assertSpecVersion(DRAFT_4, SchemaLoader.builder().schemaJson(get("constSchema")));
48+
}
49+
50+
@Test
51+
public void unknownMetaSchema_explicitDefault() {
52+
assertSpecVersion(DRAFT_6, SchemaLoader.builder()
53+
.draftV6Support()
54+
.schemaJson(get("unknownMetaSchema")));
55+
}
56+
57+
@Test
58+
public void knownMetaSchema_explicitDefault() {
59+
assertSpecVersion(DRAFT_6, SchemaLoader.builder()
60+
.draftV7Support()
61+
.schemaJson(get("explicitSchemaVersion")));
62+
}
63+
64+
@Test
65+
public void missingMetaSchema_explicitDefault() {
66+
assertSpecVersion(DRAFT_6, SchemaLoader.builder()
67+
.draftV6Support()
68+
.schemaJson(get("constSchema")));
69+
}
70+
71+
}

0 commit comments

Comments
 (0)