Skip to content

Commit 2ef1625

Browse files
committed
id (scope) resolution
1 parent e1fb001 commit 2ef1625

File tree

3 files changed

+54
-28
lines changed

3 files changed

+54
-28
lines changed

core/src/main/java/org/everit/jsonvalidator/loader/JSONPointer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private static JSONObject executeWith(final HttpClient client, final String url)
6464
String resp = null;
6565
BufferedReader buffReader = null;
6666
InputStreamReader reader = null;
67+
System.out.println("GET: " + url);
6768
try {
6869
HttpResponse response = client.execute(new HttpGet(url));
6970
InputStream content = response.getEntity().getContent();

core/src/main/java/org/everit/jsonvalidator/loader/SchemaLoader.java

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class SchemaLoader {
8282
}
8383

8484
public static Schema load(final JSONObject schemaJson) {
85-
return new SchemaLoader(schemaJson, schemaJson).load().build();
85+
return new SchemaLoader(schemaJson.optString("id"), schemaJson, schemaJson).load().build();
8686

8787
}
8888

@@ -95,7 +95,7 @@ class TypeBasedMultiplexer {
9595

9696
private class OnTypeConsumerImpl<E> implements OnTypeConsumer<E> {
9797

98-
private final Class<?> key;
98+
protected final Class<?> key;
9999

100100
public OnTypeConsumerImpl(final Class<?> key) {
101101
this.key = key;
@@ -109,6 +109,28 @@ public TypeBasedMultiplexer then(final Consumer<E> consumer) {
109109

110110
}
111111

112+
private class IdModifyingTypeConsumerImpl extends OnTypeConsumerImpl<JSONObject> {
113+
114+
public IdModifyingTypeConsumerImpl(final Class<?> key) {
115+
super(key);
116+
}
117+
118+
@Override
119+
public TypeBasedMultiplexer then(final Consumer<JSONObject> consumer) {
120+
Consumer<JSONObject> wrapperConsumer = obj -> {
121+
String origId = id;
122+
if (obj.has("id")) {
123+
id += obj.getString("id");
124+
}
125+
consumer.accept(obj);
126+
id = origId;
127+
};
128+
actions.put(key, wrapperConsumer);
129+
return TypeBasedMultiplexer.this;
130+
}
131+
132+
}
133+
112134
private final String keyOfObj;
113135

114136
private final Object obj;
@@ -125,9 +147,16 @@ public TypeBasedMultiplexer(final Object obj) {
125147
}
126148

127149
public <E> OnTypeConsumer<E> ifIs(final Class<E> predicateClass) {
150+
if (predicateClass == JSONObject.class) {
151+
throw new IllegalArgumentException("use ifObject() instead");
152+
}
128153
return new OnTypeConsumerImpl<E>(predicateClass);
129154
}
130155

156+
public OnTypeConsumer<JSONObject> ifObject() {
157+
return new IdModifyingTypeConsumerImpl(JSONObject.class);
158+
}
159+
131160
public void orElse(final Consumer<Object> orElseConsumer) {
132161
@SuppressWarnings("unchecked")
133162
Consumer<Object> consumer = (Consumer<Object>) actions.keySet().stream()
@@ -154,15 +183,17 @@ TypeBasedMultiplexer typeMultiplexer(final String keyOfObj, final Object obj) {
154183
return new TypeBasedMultiplexer(keyOfObj, obj);
155184
}
156185

157-
// TODO private final String id;
186+
private String id = null;
158187

159188
private final JSONObject schemaJson;
160189

161190
private final JSONObject rootSchemaJson;
162191

163-
public SchemaLoader(final JSONObject schemaJson, final JSONObject rootSchemaJson) {
192+
public SchemaLoader(final String id, final JSONObject schemaJson,
193+
final JSONObject rootSchemaJson) {
164194
this.schemaJson = Objects.requireNonNull(schemaJson, "schemaJson cannot be null");
165195
this.rootSchemaJson = Objects.requireNonNull(rootSchemaJson, "rootSchemaJson cannot be null");
196+
this.id = id;
166197
}
167198

168199
private void addDependencies(final Builder builder, final JSONObject deps) {
@@ -172,7 +203,7 @@ private void addDependencies(final Builder builder, final JSONObject deps) {
172203

173204
private void addDependency(final Builder builder, final String ifPresent, final Object deps) {
174205
typeMultiplexer(deps)
175-
.ifIs(JSONObject.class).then(obj -> {
206+
.ifObject().then(obj -> {
176207
builder.schemaDependency(ifPresent, loadChild(obj).build());
177208
})
178209
.ifIs(JSONArray.class).then(propNames -> {
@@ -203,14 +234,12 @@ private ArraySchema.Builder buildArraySchema() {
203234
if (schemaJson.has("additionalItems")) {
204235
typeMultiplexer("additionalItems", schemaJson.get("additionalItems"))
205236
.ifIs(Boolean.class).then(builder::additionalItems)
206-
.ifIs(JSONObject.class)
207-
.then(jsonObj -> builder.schemaOfAdditionalItems(loadChild(jsonObj).build()))
237+
.ifObject().then(jsonObj -> builder.schemaOfAdditionalItems(loadChild(jsonObj).build()))
208238
.requireAny();
209239
}
210240
if (schemaJson.has("items")) {
211241
typeMultiplexer("items", schemaJson.get("items"))
212-
.ifIs(JSONObject.class)
213-
.then(itemSchema -> builder.allItemSchema(loadChild(itemSchema).build()))
242+
.ifObject().then(itemSchema -> builder.allItemSchema(loadChild(itemSchema).build()))
214243
.ifIs(JSONArray.class).then(arr -> buildTupleSchema(builder, arr))
215244
.requireAny();
216245
}
@@ -245,8 +274,7 @@ private ObjectSchema.Builder buildObjectSchema() {
245274
if (schemaJson.has("additionalProperties")) {
246275
typeMultiplexer("additionalProperties", schemaJson.get("additionalProperties"))
247276
.ifIs(Boolean.class).then(builder::additionalProperties)
248-
.ifIs(JSONObject.class)
249-
.then(def -> builder.schemaOfAdditionalProperties(loadChild(def).build()))
277+
.ifObject().then(def -> builder.schemaOfAdditionalProperties(loadChild(def).build()))
250278
.requireAny();
251279
}
252280
if (schemaJson.has("required")) {
@@ -297,7 +325,7 @@ private StringSchema.Builder buildStringSchema() {
297325
private void buildTupleSchema(final ArraySchema.Builder builder, final JSONArray itemSchema) {
298326
for (int i = 0; i < itemSchema.length(); ++i) {
299327
typeMultiplexer(itemSchema.get(i))
300-
.ifIs(JSONObject.class).then(schema -> builder.addItemSchema(loadChild(schema).build()))
328+
.ifObject().then(schema -> builder.addItemSchema(loadChild(schema).build()))
301329
.requireAny();
302330
}
303331
}
@@ -338,10 +366,6 @@ public Schema.Builder<?> load() {
338366
}
339367

340368
private Schema.Builder<?> loadForType(final Object type) {
341-
// typeMultiplexer(type)
342-
// .ifIs(JSONArray.class).then(a -> buildAnyOfSchemaForMultipleTypes())
343-
// .ifIs(String.class).then(this::loadForExplicitType)
344-
// .requireAny();
345369
if (type instanceof JSONArray) {
346370
return buildAnyOfSchemaForMultipleTypes();
347371
} else if (type instanceof String) {
@@ -361,7 +385,7 @@ private EnumSchema.Builder buildEnumSchema() {
361385
}
362386

363387
private Schema.Builder<?> loadChild(final JSONObject childJson) {
364-
return new SchemaLoader(childJson, rootSchemaJson).load();
388+
return new SchemaLoader(id, childJson, rootSchemaJson).load();
365389
}
366390

367391
private Schema.Builder<?> loadForExplicitType(final String typeString) {
@@ -385,18 +409,19 @@ private Schema.Builder<?> loadForExplicitType(final String typeString) {
385409
}
386410
}
387411

388-
private Schema.Builder<?> lookupReference(final String pointerString) {
389-
if (pointerString.equals("#")) {
412+
private Schema.Builder<?> lookupReference(final String relPointerString) {
413+
if (relPointerString.equals("#")) {
390414
throw new UnsupportedOperationException("recursive reference");
391415
}
392416
JSONPointer pointer;
393-
if (pointerString.startsWith("#")) {
394-
pointer = JSONPointer.forDocument(rootSchemaJson, pointerString);
417+
String absPointerString = id + relPointerString;
418+
if (absPointerString.startsWith("#")) {
419+
pointer = JSONPointer.forDocument(rootSchemaJson, absPointerString);
395420
} else {
396-
pointer = JSONPointer.forURL(HttpClients.createDefault(), pointerString);
421+
pointer = JSONPointer.forURL(HttpClients.createDefault(), absPointerString);
397422
}
398423
QueryResult result = pointer.query();
399-
return new SchemaLoader(result.getQueryResult(), result.getContainingDocument()).load();
424+
return new SchemaLoader(id, result.getQueryResult(), result.getContainingDocument()).load();
400425
}
401426

402427
private boolean schemaHasAnyOf(final Collection<String> propNames) {

core/src/test/java/org/everit/jsonvalidator/loader/SchemaLoaderTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ public class SchemaLoaderTest {
4444

4545
@Test
4646
public void typeBasedMultiplexerTest() {
47-
SchemaLoader loader = new SchemaLoader(new JSONObject(), new JSONObject());
47+
SchemaLoader loader = new SchemaLoader(null, new JSONObject(), new JSONObject());
4848
loader.typeMultiplexer(new JSONObject())
49-
.ifIs(JSONObject.class).then(jsonObj -> {
49+
.ifObject().then(jsonObj -> {
5050
})
5151
.ifIs(JSONArray.class).then(jsonArr -> {
5252
})
5353
.orElse(obj -> {
5454
});
5555

5656
loader.typeMultiplexer(new JSONObject())
57-
.ifIs(JSONObject.class).then(jsonObj -> {
57+
.ifObject().then(jsonObj -> {
5858
})
5959
.ifIs(JSONArray.class).then(jsonArr -> {
6060
})
@@ -63,9 +63,9 @@ public void typeBasedMultiplexerTest() {
6363

6464
@Test(expected = SchemaException.class)
6565
public void typeBasedMultiplexerFailure() {
66-
SchemaLoader loader = new SchemaLoader(new JSONObject(), new JSONObject());
66+
SchemaLoader loader = new SchemaLoader(null, new JSONObject(), new JSONObject());
6767
loader.typeMultiplexer("foo")
68-
.ifIs(JSONObject.class).then(o -> {
68+
.ifObject().then(o -> {
6969
})
7070
.ifIs(JSONArray.class).then(o -> {
7171
})

0 commit comments

Comments
 (0)