Skip to content

Commit bb81ad1

Browse files
committed
fixing #41
1 parent 17d8ed4 commit bb81ad1

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ public Schema.Builder<?> load() {
513513
} else {
514514
builder = tryCombinedSchema();
515515
if (builder == null) {
516-
if (!schemaJson.has("type")) {
516+
if (!schemaJson.has("type") || schemaJson.has("$ref")) {
517517
builder = buildSchemaWithoutExplicitType();
518518
} else {
519519
builder = loadForType(schemaJson.get("type"));
@@ -578,7 +578,7 @@ private Schema.Builder<?> lookupReference(final String relPointerString, final J
578578
QueryResult result = pointer.query();
579579
JSONObject resultObject = extend(withoutRef(ctx), result.getQueryResult());
580580
SchemaLoader childLoader =
581-
selfBuilder().resolutionScope(isExternal ? withoutFragment(absPointerString) : id)
581+
selfBuilder().resolutionScope(isExternal ? withoutFragment(absPointerString) : id)
582582
.schemaJson(resultObject)
583583
.rootSchemaJson(result.getContainingDocument()).build();
584584
Schema referredSchema = childLoader.load().build();
@@ -679,7 +679,7 @@ JSONObject withoutRef(final JSONObject original) {
679679
}
680680
JSONObject rval = new JSONObject();
681681
Arrays.stream(names)
682-
.filter(name -> !"$ref".equals(name))
682+
.filter(name -> !"$ref".equals(name))
683683
.forEach(name -> rval.put(name, original.get(name)));
684684
return rval;
685685
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.ByteArrayInputStream;
1919
import java.io.InputStream;
20+
import java.util.Arrays;
2021
import java.util.Map;
2122
import java.util.Optional;
2223

@@ -101,13 +102,14 @@ public void booleanSchema() {
101102
public void builderhasDefaultFormatValidators() {
102103
SchemaLoader actual = SchemaLoader.builder().schemaJson(get("booleanSchema")).build();
103104
Assert
104-
.assertTrue(actual.getFormatValidator("date-time").get() instanceof DateTimeFormatValidator);
105+
.assertTrue(
106+
actual.getFormatValidator("date-time").get() instanceof DateTimeFormatValidator);
105107
Assert.assertTrue(actual.getFormatValidator("uri").get() instanceof URIFormatValidator);
106108
Assert.assertTrue(actual.getFormatValidator("email").get() instanceof EmailFormatValidator);
107109
Assert.assertTrue(actual.getFormatValidator("ipv4").get() instanceof IPV4Validator);
108110
Assert.assertTrue(actual.getFormatValidator("ipv6").get() instanceof IPV6Validator);
109111
Assert
110-
.assertTrue(actual.getFormatValidator("hostname").get() instanceof HostnameFormatValidator);
112+
.assertTrue(actual.getFormatValidator("hostname").get() instanceof HostnameFormatValidator);
111113
}
112114

113115
@Test
@@ -327,7 +329,7 @@ public void pointerResolution() {
327329
ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("pointerResolution"));
328330
ObjectSchema rectangleSchema = (ObjectSchema) ((ReferenceSchema) actual.getPropertySchemas()
329331
.get("rectangle"))
330-
.getReferredSchema();
332+
.getReferredSchema();
331333
Assert.assertNotNull(rectangleSchema);
332334
ReferenceSchema aRef = (ReferenceSchema) rectangleSchema.getPropertySchemas().get("a");
333335
Assert.assertTrue(aRef.getReferredSchema() instanceof NumberSchema);
@@ -353,6 +355,14 @@ public void recursiveSchema() {
353355
SchemaLoader.load(get("recursiveSchema"));
354356
}
355357

358+
@Test
359+
public void refWithType() {
360+
ObjectSchema actualRoot = (ObjectSchema) SchemaLoader.load(get("refWithType"));
361+
ReferenceSchema actual = (ReferenceSchema) actualRoot.getPropertySchemas().get("prop");
362+
ObjectSchema propSchema = (ObjectSchema) actual.getReferredSchema();
363+
Assert.assertEquals(propSchema.getRequiredProperties(), Arrays.asList("a", "b"));
364+
}
365+
356366
@Test
357367
public void remotePointerResulion() {
358368
SchemaClient httpClient = Mockito.mock(SchemaClient.class);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,19 @@
253253
}
254254
}
255255
},
256+
"refWithType" : {
257+
"properties" : {
258+
"prop" : {
259+
"type": "object",
260+
"$ref" : "#/definitions/PropDef"
261+
}
262+
},
263+
"definitions" : {
264+
"PropDef" : {
265+
"required" : ["a", "b"]
266+
}
267+
}
268+
},
256269
"resolutionScopeTest" : {
257270
"id": "http://x.y.z/rootschema.json#",
258271
"schema1": {

0 commit comments

Comments
 (0)