From 3742d921cd8b332f3d1314f6e2c49b5446c3fda6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bence=20Er=C5=91s?= Date: Sat, 16 Apr 2016 17:17:34 +0200 Subject: [PATCH] handling root "id" as default resolution scope in constructor instead of in preceding load() --- .../everit/json/schema/loader/SchemaLoader.java | 14 +++++++++++--- .../json/schema/loader/SchemaLoaderTest.java | 10 ++++++++++ .../org/everit/jsonvalidator/testschemas.json | 7 ++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java b/core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java index 577e3185e..5430e1707 100644 --- a/core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java +++ b/core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java @@ -59,6 +59,7 @@ import org.everit.json.schema.loader.internal.ReferenceResolver; import org.everit.json.schema.loader.internal.TypeBasedMultiplexer; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; /** @@ -215,8 +216,7 @@ public static Schema load(final JSONObject schemaJson) { * @return the created schema */ public static Schema load(final JSONObject schemaJson, final SchemaClient httpClient) { - String schemaId = schemaJson.optString("id"); - SchemaLoader loader = builder().resolutionScope(schemaId) + SchemaLoader loader = builder() .schemaJson(schemaJson) .httpClient(httpClient) .build(); @@ -271,7 +271,15 @@ public SchemaLoader(final SchemaLoaderBuilder builder) { this.schemaJson = Objects.requireNonNull(builder.schemaJson, "schemaJson cannot be null"); this.rootSchemaJson = Objects.requireNonNull(builder.getRootSchemaJson(), "rootSchemaJson cannot be null"); - this.id = builder.id; + URI id = builder.id; + if (id == null && builder.schemaJson.has("id")) { + try { + id = new URI(builder.schemaJson.getString("id")); + } catch (JSONException | URISyntaxException e) { + throw new RuntimeException(e); + } + } + this.id = id; this.httpClient = Objects.requireNonNull(builder.httpClient, "httpClient cannot be null"); this.pointerSchemas = Objects.requireNonNull(builder.pointerSchemas, "pointerSchemas cannot be null"); diff --git a/core/src/test/java/org/everit/json/schema/loader/SchemaLoaderTest.java b/core/src/test/java/org/everit/json/schema/loader/SchemaLoaderTest.java index 698e76855..dfe3fc397 100644 --- a/core/src/test/java/org/everit/json/schema/loader/SchemaLoaderTest.java +++ b/core/src/test/java/org/everit/json/schema/loader/SchemaLoaderTest.java @@ -425,6 +425,16 @@ public void unsupportedFormat() { SchemaLoader.builder().schemaJson(schema).build().load(); } + @Test + public void schemaJsonIdIsRecognized() { + SchemaClient client = Mockito.mock(SchemaClient.class); + ByteArrayInputStream retval = new ByteArrayInputStream("{}".getBytes()); + Mockito.when(client.get("http://example.org/schema/schema.json")).thenReturn(retval); + SchemaLoader.builder().schemaJson(get("schemaWithId")) + .httpClient(client) + .build().load(); + } + @Test public void withoutFragment() { String actual = SchemaLoader.withoutFragment("http://example.com#frag").toString(); diff --git a/core/src/test/resources/org/everit/jsonvalidator/testschemas.json b/core/src/test/resources/org/everit/jsonvalidator/testschemas.json index 5f5ab1ae9..b04898e4f 100644 --- a/core/src/test/resources/org/everit/jsonvalidator/testschemas.json +++ b/core/src/test/resources/org/everit/jsonvalidator/testschemas.json @@ -303,6 +303,11 @@ "minProperties" : 1 } } - + }, + "schemaWithId" : { + "id" : "http://example.org/schema/", + "properties" : { + "prop" : { "$ref" : "schema.json" } + } } }