@@ -82,7 +82,7 @@ public class SchemaLoader {
82
82
}
83
83
84
84
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 ();
86
86
87
87
}
88
88
@@ -95,7 +95,7 @@ class TypeBasedMultiplexer {
95
95
96
96
private class OnTypeConsumerImpl <E > implements OnTypeConsumer <E > {
97
97
98
- private final Class <?> key ;
98
+ protected final Class <?> key ;
99
99
100
100
public OnTypeConsumerImpl (final Class <?> key ) {
101
101
this .key = key ;
@@ -109,6 +109,28 @@ public TypeBasedMultiplexer then(final Consumer<E> consumer) {
109
109
110
110
}
111
111
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
+
112
134
private final String keyOfObj ;
113
135
114
136
private final Object obj ;
@@ -125,9 +147,16 @@ public TypeBasedMultiplexer(final Object obj) {
125
147
}
126
148
127
149
public <E > OnTypeConsumer <E > ifIs (final Class <E > predicateClass ) {
150
+ if (predicateClass == JSONObject .class ) {
151
+ throw new IllegalArgumentException ("use ifObject() instead" );
152
+ }
128
153
return new OnTypeConsumerImpl <E >(predicateClass );
129
154
}
130
155
156
+ public OnTypeConsumer <JSONObject > ifObject () {
157
+ return new IdModifyingTypeConsumerImpl (JSONObject .class );
158
+ }
159
+
131
160
public void orElse (final Consumer <Object > orElseConsumer ) {
132
161
@ SuppressWarnings ("unchecked" )
133
162
Consumer <Object > consumer = (Consumer <Object >) actions .keySet ().stream ()
@@ -154,15 +183,17 @@ TypeBasedMultiplexer typeMultiplexer(final String keyOfObj, final Object obj) {
154
183
return new TypeBasedMultiplexer (keyOfObj , obj );
155
184
}
156
185
157
- // TODO private final String id;
186
+ private String id = null ;
158
187
159
188
private final JSONObject schemaJson ;
160
189
161
190
private final JSONObject rootSchemaJson ;
162
191
163
- public SchemaLoader (final JSONObject schemaJson , final JSONObject rootSchemaJson ) {
192
+ public SchemaLoader (final String id , final JSONObject schemaJson ,
193
+ final JSONObject rootSchemaJson ) {
164
194
this .schemaJson = Objects .requireNonNull (schemaJson , "schemaJson cannot be null" );
165
195
this .rootSchemaJson = Objects .requireNonNull (rootSchemaJson , "rootSchemaJson cannot be null" );
196
+ this .id = id ;
166
197
}
167
198
168
199
private void addDependencies (final Builder builder , final JSONObject deps ) {
@@ -172,7 +203,7 @@ private void addDependencies(final Builder builder, final JSONObject deps) {
172
203
173
204
private void addDependency (final Builder builder , final String ifPresent , final Object deps ) {
174
205
typeMultiplexer (deps )
175
- .ifIs ( JSONObject . class ).then (obj -> {
206
+ .ifObject ( ).then (obj -> {
176
207
builder .schemaDependency (ifPresent , loadChild (obj ).build ());
177
208
})
178
209
.ifIs (JSONArray .class ).then (propNames -> {
@@ -203,14 +234,12 @@ private ArraySchema.Builder buildArraySchema() {
203
234
if (schemaJson .has ("additionalItems" )) {
204
235
typeMultiplexer ("additionalItems" , schemaJson .get ("additionalItems" ))
205
236
.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 ()))
208
238
.requireAny ();
209
239
}
210
240
if (schemaJson .has ("items" )) {
211
241
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 ()))
214
243
.ifIs (JSONArray .class ).then (arr -> buildTupleSchema (builder , arr ))
215
244
.requireAny ();
216
245
}
@@ -245,8 +274,7 @@ private ObjectSchema.Builder buildObjectSchema() {
245
274
if (schemaJson .has ("additionalProperties" )) {
246
275
typeMultiplexer ("additionalProperties" , schemaJson .get ("additionalProperties" ))
247
276
.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 ()))
250
278
.requireAny ();
251
279
}
252
280
if (schemaJson .has ("required" )) {
@@ -297,7 +325,7 @@ private StringSchema.Builder buildStringSchema() {
297
325
private void buildTupleSchema (final ArraySchema .Builder builder , final JSONArray itemSchema ) {
298
326
for (int i = 0 ; i < itemSchema .length (); ++i ) {
299
327
typeMultiplexer (itemSchema .get (i ))
300
- .ifIs ( JSONObject . class ).then (schema -> builder .addItemSchema (loadChild (schema ).build ()))
328
+ .ifObject ( ).then (schema -> builder .addItemSchema (loadChild (schema ).build ()))
301
329
.requireAny ();
302
330
}
303
331
}
@@ -338,10 +366,6 @@ public Schema.Builder<?> load() {
338
366
}
339
367
340
368
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();
345
369
if (type instanceof JSONArray ) {
346
370
return buildAnyOfSchemaForMultipleTypes ();
347
371
} else if (type instanceof String ) {
@@ -361,7 +385,7 @@ private EnumSchema.Builder buildEnumSchema() {
361
385
}
362
386
363
387
private Schema .Builder <?> loadChild (final JSONObject childJson ) {
364
- return new SchemaLoader (childJson , rootSchemaJson ).load ();
388
+ return new SchemaLoader (id , childJson , rootSchemaJson ).load ();
365
389
}
366
390
367
391
private Schema .Builder <?> loadForExplicitType (final String typeString ) {
@@ -385,18 +409,19 @@ private Schema.Builder<?> loadForExplicitType(final String typeString) {
385
409
}
386
410
}
387
411
388
- private Schema .Builder <?> lookupReference (final String pointerString ) {
389
- if (pointerString .equals ("#" )) {
412
+ private Schema .Builder <?> lookupReference (final String relPointerString ) {
413
+ if (relPointerString .equals ("#" )) {
390
414
throw new UnsupportedOperationException ("recursive reference" );
391
415
}
392
416
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 );
395
420
} else {
396
- pointer = JSONPointer .forURL (HttpClients .createDefault (), pointerString );
421
+ pointer = JSONPointer .forURL (HttpClients .createDefault (), absPointerString );
397
422
}
398
423
QueryResult result = pointer .query ();
399
- return new SchemaLoader (result .getQueryResult (), result .getContainingDocument ()).load ();
424
+ return new SchemaLoader (id , result .getQueryResult (), result .getContainingDocument ()).load ();
400
425
}
401
426
402
427
private boolean schemaHasAnyOf (final Collection <String > propNames ) {
0 commit comments