@@ -69,10 +69,13 @@ public class SchemaLoader {
69
69
*/
70
70
@ FunctionalInterface
71
71
private interface CombinedSchemaProvider
72
- extends Function <Collection <Schema >, CombinedSchema .Builder > {
72
+ extends Function <Collection <Schema >, CombinedSchema .Builder > {
73
73
74
74
}
75
75
76
+ /**
77
+ * Builder class for {@link SchemaLoader}.
78
+ */
76
79
public static class SchemaLoaderBuilder {
77
80
78
81
SchemaClient httpClient = new DefaultSchemaClient ();
@@ -209,6 +212,9 @@ public static Schema load(final JSONObject schemaJson, final SchemaClient httpCl
209
212
210
213
private final Map <String , FormatValidator > formatValidators ;
211
214
215
+ /**
216
+ * Constructor.
217
+ */
212
218
public SchemaLoader (final SchemaLoaderBuilder builder ) {
213
219
this .schemaJson = Objects .requireNonNull (builder .schemaJson , "schemaJson cannot be null" );
214
220
this .rootSchemaJson = Objects .requireNonNull (builder .getRootSchemaJson (),
@@ -239,19 +245,19 @@ public SchemaLoader(final SchemaLoaderBuilder builder) {
239
245
240
246
private void addDependencies (final Builder builder , final JSONObject deps ) {
241
247
Arrays .stream (JSONObject .getNames (deps ))
242
- .forEach (ifPresent -> addDependency (builder , ifPresent , deps .get (ifPresent )));
248
+ .forEach (ifPresent -> addDependency (builder , ifPresent , deps .get (ifPresent )));
243
249
}
244
250
245
251
private void addDependency (final Builder builder , final String ifPresent , final Object deps ) {
246
252
typeMultiplexer (deps )
247
- .ifObject ().then (obj -> {
248
- builder .schemaDependency (ifPresent , loadChild (obj ).build ());
249
- })
250
- .ifIs (JSONArray .class ).then (propNames -> {
251
- IntStream .range (0 , propNames .length ())
252
- .mapToObj (i -> propNames .getString (i ))
253
- .forEach (dependency -> builder .propertyDependency (ifPresent , dependency ));
254
- }).requireAny ();
253
+ .ifObject ().then (obj -> {
254
+ builder .schemaDependency (ifPresent , loadChild (obj ).build ());
255
+ })
256
+ .ifIs (JSONArray .class ).then (propNames -> {
257
+ IntStream .range (0 , propNames .length ())
258
+ .mapToObj (i -> propNames .getString (i ))
259
+ .forEach (dependency -> builder .propertyDependency (ifPresent , dependency ));
260
+ }).requireAny ();
255
261
}
256
262
257
263
private void addFormatValidator (final StringSchema .Builder builder , final String formatName ) {
@@ -261,11 +267,11 @@ private void addFormatValidator(final StringSchema.Builder builder, final String
261
267
private void addPropertySchemaDefinition (final String keyOfObj , final Object definition ,
262
268
final ObjectSchema .Builder builder ) {
263
269
typeMultiplexer (definition )
264
- .ifObject ()
265
- .then (obj -> {
266
- builder .addPropertySchema (keyOfObj , loadChild (obj ).build ());
267
- })
268
- .requireAny ();
270
+ .ifObject ()
271
+ .then (obj -> {
272
+ builder .addPropertySchema (keyOfObj , loadChild (obj ).build ());
273
+ })
274
+ .requireAny ();
269
275
}
270
276
271
277
private CombinedSchema .Builder buildAnyOfSchemaForMultipleTypes () {
@@ -288,15 +294,15 @@ private ArraySchema.Builder buildArraySchema() {
288
294
ifPresent ("uniqueItems" , Boolean .class , builder ::uniqueItems );
289
295
if (schemaJson .has ("additionalItems" )) {
290
296
typeMultiplexer ("additionalItems" , schemaJson .get ("additionalItems" ))
291
- .ifIs (Boolean .class ).then (builder ::additionalItems )
292
- .ifObject ().then (jsonObj -> builder .schemaOfAdditionalItems (loadChild (jsonObj ).build ()))
293
- .requireAny ();
297
+ .ifIs (Boolean .class ).then (builder ::additionalItems )
298
+ .ifObject ().then (jsonObj -> builder .schemaOfAdditionalItems (loadChild (jsonObj ).build ()))
299
+ .requireAny ();
294
300
}
295
301
if (schemaJson .has ("items" )) {
296
302
typeMultiplexer ("items" , schemaJson .get ("items" ))
297
- .ifObject ().then (itemSchema -> builder .allItemSchema (loadChild (itemSchema ).build ()))
298
- .ifIs (JSONArray .class ).then (arr -> buildTupleSchema (builder , arr ))
299
- .requireAny ();
303
+ .ifObject ().then (itemSchema -> builder .allItemSchema (loadChild (itemSchema ).build ()))
304
+ .ifIs (JSONArray .class ).then (arr -> buildTupleSchema (builder , arr ))
305
+ .requireAny ();
300
306
}
301
307
return builder ;
302
308
}
@@ -305,8 +311,8 @@ private EnumSchema.Builder buildEnumSchema() {
305
311
Set <Object > possibleValues = new HashSet <>();
306
312
JSONArray arr = schemaJson .getJSONArray ("enum" );
307
313
IntStream .range (0 , arr .length ())
308
- .mapToObj (arr ::get )
309
- .forEach (possibleValues ::add );
314
+ .mapToObj (arr ::get )
315
+ .forEach (possibleValues ::add );
310
316
return EnumSchema .builder ().possibleValues (possibleValues );
311
317
}
312
318
@@ -331,21 +337,21 @@ private ObjectSchema.Builder buildObjectSchema() {
331
337
ifPresent ("maxProperties" , Integer .class , builder ::maxProperties );
332
338
if (schemaJson .has ("properties" )) {
333
339
typeMultiplexer (schemaJson .get ("properties" ))
334
- .ifObject ().then (propertyDefs -> {
335
- populatePropertySchemas (propertyDefs , builder );
336
- }).requireAny ();
340
+ .ifObject ().then (propertyDefs -> {
341
+ populatePropertySchemas (propertyDefs , builder );
342
+ }).requireAny ();
337
343
}
338
344
if (schemaJson .has ("additionalProperties" )) {
339
345
typeMultiplexer ("additionalProperties" , schemaJson .get ("additionalProperties" ))
340
- .ifIs (Boolean .class ).then (builder ::additionalProperties )
341
- .ifObject ().then (def -> builder .schemaOfAdditionalProperties (loadChild (def ).build ()))
342
- .requireAny ();
346
+ .ifIs (Boolean .class ).then (builder ::additionalProperties )
347
+ .ifObject ().then (def -> builder .schemaOfAdditionalProperties (loadChild (def ).build ()))
348
+ .requireAny ();
343
349
}
344
350
if (schemaJson .has ("required" )) {
345
351
JSONArray requiredJson = schemaJson .getJSONArray ("required" );
346
352
IntStream .range (0 , requiredJson .length ())
347
- .mapToObj (requiredJson ::getString )
348
- .forEach (builder ::addRequiredProperty );
353
+ .mapToObj (requiredJson ::getString )
354
+ .forEach (builder ::addRequiredProperty );
349
355
}
350
356
if (schemaJson .has ("patternProperties" )) {
351
357
JSONObject patternPropsJson = schemaJson .getJSONObject ("patternProperties" );
@@ -390,8 +396,8 @@ private StringSchema.Builder buildStringSchema() {
390
396
private void buildTupleSchema (final ArraySchema .Builder builder , final JSONArray itemSchema ) {
391
397
for (int i = 0 ; i < itemSchema .length (); ++i ) {
392
398
typeMultiplexer (itemSchema .get (i ))
393
- .ifObject ().then (schema -> builder .addItemSchema (loadChild (schema ).build ()))
394
- .requireAny ();
399
+ .ifObject ().then (schema -> builder .addItemSchema (loadChild (schema ).build ()))
400
+ .requireAny ();
395
401
}
396
402
}
397
403
@@ -505,16 +511,16 @@ private Schema.Builder<?> lookupReference(final String relPointerString, final J
505
511
}
506
512
JSONPointer pointer = absPointerString .startsWith ("#" )
507
513
? JSONPointer .forDocument (rootSchemaJson , absPointerString )
508
- : JSONPointer .forURL (httpClient , absPointerString );
509
- ReferenceSchema .Builder refBuilder = ReferenceSchema .builder ();
510
- pointerSchemas .put (absPointerString , refBuilder );
511
- QueryResult result = pointer .query ();
512
- JSONObject resultObject = extend (withoutRef (ctx ), result .getQueryResult ());
513
- SchemaLoader childLoader = selfBuilder ().schemaJson (resultObject )
514
- .rootSchemaJson (result .getContainingDocument ()).build ();
515
- Schema referredSchema = childLoader .load ().build ();
516
- refBuilder .build ().setReferredSchema (referredSchema );
517
- return refBuilder ;
514
+ : JSONPointer .forURL (httpClient , absPointerString );
515
+ ReferenceSchema .Builder refBuilder = ReferenceSchema .builder ();
516
+ pointerSchemas .put (absPointerString , refBuilder );
517
+ QueryResult result = pointer .query ();
518
+ JSONObject resultObject = extend (withoutRef (ctx ), result .getQueryResult ());
519
+ SchemaLoader childLoader = selfBuilder ().schemaJson (resultObject )
520
+ .rootSchemaJson (result .getContainingDocument ()).build ();
521
+ Schema referredSchema = childLoader .load ().build ();
522
+ refBuilder .build ().setReferredSchema (referredSchema );
523
+ return refBuilder ;
518
524
}
519
525
520
526
private void populatePropertySchemas (final JSONObject propertyDefs ,
@@ -608,8 +614,8 @@ JSONObject withoutRef(final JSONObject original) {
608
614
}
609
615
JSONObject rval = new JSONObject ();
610
616
Arrays .stream (names )
611
- .filter (name -> !"$ref" .equals (name ))
612
- .forEach (name -> rval .put (name , original .get (name )));
617
+ .filter (name -> !"$ref" .equals (name ))
618
+ .forEach (name -> rval .put (name , original .get (name )));
613
619
return rval ;
614
620
}
615
621
}
0 commit comments