Skip to content

Commit

Permalink
handling root "id" as default resolution scope in constructor instead…
Browse files Browse the repository at this point in the history
… of in preceding load()
  • Loading branch information
erosb committed Apr 16, 2016
1 parent 07f4229 commit 3742d92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
14 changes: 11 additions & 3 deletions core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@
"minProperties" : 1
}
}

},
"schemaWithId" : {
"id" : "http://example.org/schema/",
"properties" : {
"prop" : { "$ref" : "schema.json" }
}
}
}

0 comments on commit 3742d92

Please sign in to comment.