Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/io/vertx/json/schema/impl/JsonRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ private static JsonObject resolveUri(String uri, Map<String, JsonObject> anchors
// [prefix, path]
final String[] parts = uri.split("#", 2);

final boolean hashPresent = parts.length == 2 && parts[1] != null;
// an empty fragment ("#" or "id#") is equivalent to no fragment at all: the uri refers
// to the schema identified by the prefix, e.g.: the root schema when the prefix is empty
final boolean hashPresent = parts.length == 2 && parts[1] != null && !parts[1].isEmpty();

final String prefix = parts[0];
final String path = hashPresent ? parts[1] : null;
Expand Down
77 changes: 77 additions & 0 deletions src/test/java/io/vertx/tests/impl/RefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,83 @@ public void testCase2() {
"string");
}

@Test
public void testRefToRoot() {
JsonObject schema = new JsonObject()
.put("type", "object")
.put("properties", new JsonObject()
.put("children", new JsonObject()
.put("type", "array")
.put("items", new JsonObject()
.put("$ref", "#"))));

JsonObject resolved = JsonRef.resolve(schema);

JsonObject items = resolved
.getJsonObject("properties")
.getJsonObject("children")
.getJsonObject("items");
assertEquals("object", items.getString("type"));
// the ref points back to the root schema itself
assertNotNull(
items
.getJsonObject("properties")
.getJsonObject("children")
.getJsonObject("items"));
}

@Test
public void testRefToRootWithId() {
JsonObject schema = new JsonObject()
.put("$id", "http://www.example.com/tree/")
.put("type", "object")
.put("properties", new JsonObject()
.put("children", new JsonObject()
.put("type", "array")
.put("items", new JsonObject()
.put("$ref", "#"))));

JsonObject resolved = JsonRef.resolve(schema);

JsonObject items = resolved
.getJsonObject("properties")
.getJsonObject("children")
.getJsonObject("items");
assertEquals("object", items.getString("type"));
assertNotNull(
items
.getJsonObject("properties")
.getJsonObject("children")
.getJsonObject("items"));
}

@Test
public void testRefToRootThroughRepository() {
SchemaRepository repo =
SchemaRepository.create(new JsonSchemaOptions().setBaseUri("http://vertx.io").setDraft(Draft.DRAFT4));
JsonObject schema = new JsonObject()
.put("type", "object")
.put("properties", new JsonObject()
.put("children", new JsonObject()
.put("type", "array")
.put("items", new JsonObject()
.put("$ref", "#"))));
repo.dereference(JsonSchema.of(schema.copy()));

JsonObject resolved = repo.resolve(schema);

JsonObject items = resolved
.getJsonObject("properties")
.getJsonObject("children")
.getJsonObject("items");
assertEquals("object", items.getString("type"));
assertNotNull(
items
.getJsonObject("properties")
.getJsonObject("children")
.getJsonObject("items"));
}

@Test
void testSerialization() {
SchemaRepository repo =
Expand Down