Skip to content

Commit ca3b5f4

Browse files
committed
adding more tests & following relative URIs during schema loading
1 parent 1ac33c6 commit ca3b5f4

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,28 @@ public static Schema load(final JSONObject schemaJson, final SchemaClient httpCl
223223
return loader.load().build();
224224
}
225225

226+
/**
227+
* Returns the absolute URI without its fragment part.
228+
*
229+
* @param fullUri
230+
* the abslute URI
231+
* @return the URI without the fragment part
232+
*/
233+
static URI withoutFragment(final String fullUri) {
234+
int hashmarkIdx = fullUri.indexOf('#');
235+
String rval;
236+
if (hashmarkIdx == -1) {
237+
rval = fullUri;
238+
} else {
239+
rval = fullUri.substring(0, hashmarkIdx);
240+
}
241+
try {
242+
return new URI(rval);
243+
} catch (URISyntaxException e) {
244+
throw new RuntimeException(e);
245+
}
246+
}
247+
226248
private final SchemaClient httpClient;
227249

228250
private URI id = null;
@@ -547,8 +569,9 @@ private Schema.Builder<?> lookupReference(final String relPointerString, final J
547569
pointerSchemas.put(absPointerString, refBuilder);
548570
QueryResult result = pointer.query();
549571
JSONObject resultObject = extend(withoutRef(ctx), result.getQueryResult());
550-
SchemaLoader childLoader = selfBuilder().resolutionScope(isExternal ? null : id)
551-
.schemaJson(resultObject)
572+
SchemaLoader childLoader =
573+
selfBuilder().resolutionScope(isExternal ? withoutFragment(absPointerString) : id)
574+
.schemaJson(resultObject)
552575
.rootSchemaJson(result.getContainingDocument()).build();
553576
Schema referredSchema = childLoader.load().build();
554577
refBuilder.build().setReferredSchema(referredSchema);

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public void booleanSchema() {
101101
public void builderhasDefaultFormatValidators() {
102102
SchemaLoader actual = SchemaLoader.builder().schemaJson(get("booleanSchema")).build();
103103
Assert
104-
.assertTrue(actual.getFormatValidator("date-time").get() instanceof DateTimeFormatValidator);
104+
.assertTrue(actual.getFormatValidator("date-time").get() instanceof DateTimeFormatValidator);
105105
Assert.assertTrue(actual.getFormatValidator("uri").get() instanceof URIFormatValidator);
106106
Assert.assertTrue(actual.getFormatValidator("email").get() instanceof EmailFormatValidator);
107107
Assert.assertTrue(actual.getFormatValidator("ipv4").get() instanceof IPV4Validator);
108108
Assert.assertTrue(actual.getFormatValidator("ipv6").get() instanceof IPV6Validator);
109109
Assert
110-
.assertTrue(actual.getFormatValidator("hostname").get() instanceof HostnameFormatValidator);
110+
.assertTrue(actual.getFormatValidator("hostname").get() instanceof HostnameFormatValidator);
111111
}
112112

113113
@Test
@@ -424,4 +424,16 @@ public void unsupportedFormat() {
424424
schema.put("format", "unknown");
425425
SchemaLoader.builder().schemaJson(schema).build().load();
426426
}
427+
428+
@Test
429+
public void withoutFragment() {
430+
String actual = SchemaLoader.withoutFragment("http://example.com#frag").toString();
431+
Assert.assertEquals("http://example.com", actual);
432+
}
433+
434+
@Test
435+
public void withoutFragmentNoFragment() {
436+
String actual = SchemaLoader.withoutFragment("http://example.com").toString();
437+
Assert.assertEquals("http://example.com", actual);
438+
}
427439
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"definitions" : {
3+
"Identifier" : {
4+
"type" : "string",
5+
"maxLength" : 10
6+
}
7+
}
8+
}

tests/src/test/resources/org/everit/json/schema/relative-uri/schema/props.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"type" : "object",
55
"properties" : {
66
"a" : { "$ref" : "#/definitions/Size" },
7-
"b" : { "$ref" : "#/definitions/Size" }
7+
"b" : { "$ref" : "#/definitions/Size" },
8+
"id" : { "$ref" : "directory/subschemas.json#/definitions/Identifier"}
89
}
910
},
1011
"Size" : {

0 commit comments

Comments
 (0)