Skip to content

Commit 3742d92

Browse files
committed
handling root "id" as default resolution scope in constructor instead of in preceding load()
1 parent 07f4229 commit 3742d92

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.everit.json.schema.loader.internal.ReferenceResolver;
6060
import org.everit.json.schema.loader.internal.TypeBasedMultiplexer;
6161
import org.json.JSONArray;
62+
import org.json.JSONException;
6263
import org.json.JSONObject;
6364

6465
/**
@@ -215,8 +216,7 @@ public static Schema load(final JSONObject schemaJson) {
215216
* @return the created schema
216217
*/
217218
public static Schema load(final JSONObject schemaJson, final SchemaClient httpClient) {
218-
String schemaId = schemaJson.optString("id");
219-
SchemaLoader loader = builder().resolutionScope(schemaId)
219+
SchemaLoader loader = builder()
220220
.schemaJson(schemaJson)
221221
.httpClient(httpClient)
222222
.build();
@@ -271,7 +271,15 @@ public SchemaLoader(final SchemaLoaderBuilder builder) {
271271
this.schemaJson = Objects.requireNonNull(builder.schemaJson, "schemaJson cannot be null");
272272
this.rootSchemaJson = Objects.requireNonNull(builder.getRootSchemaJson(),
273273
"rootSchemaJson cannot be null");
274-
this.id = builder.id;
274+
URI id = builder.id;
275+
if (id == null && builder.schemaJson.has("id")) {
276+
try {
277+
id = new URI(builder.schemaJson.getString("id"));
278+
} catch (JSONException | URISyntaxException e) {
279+
throw new RuntimeException(e);
280+
}
281+
}
282+
this.id = id;
275283
this.httpClient = Objects.requireNonNull(builder.httpClient, "httpClient cannot be null");
276284
this.pointerSchemas = Objects.requireNonNull(builder.pointerSchemas,
277285
"pointerSchemas cannot be null");

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,16 @@ public void unsupportedFormat() {
425425
SchemaLoader.builder().schemaJson(schema).build().load();
426426
}
427427

428+
@Test
429+
public void schemaJsonIdIsRecognized() {
430+
SchemaClient client = Mockito.mock(SchemaClient.class);
431+
ByteArrayInputStream retval = new ByteArrayInputStream("{}".getBytes());
432+
Mockito.when(client.get("http://example.org/schema/schema.json")).thenReturn(retval);
433+
SchemaLoader.builder().schemaJson(get("schemaWithId"))
434+
.httpClient(client)
435+
.build().load();
436+
}
437+
428438
@Test
429439
public void withoutFragment() {
430440
String actual = SchemaLoader.withoutFragment("http://example.com#frag").toString();

core/src/test/resources/org/everit/jsonvalidator/testschemas.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@
303303
"minProperties" : 1
304304
}
305305
}
306-
306+
},
307+
"schemaWithId" : {
308+
"id" : "http://example.org/schema/",
309+
"properties" : {
310+
"prop" : { "$ref" : "schema.json" }
311+
}
307312
}
308313
}

0 commit comments

Comments
 (0)