@@ -69,7 +69,7 @@ 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
@@ -138,6 +138,11 @@ public SchemaLoaderBuilder schemaJson(final JSONObject schemaJson) {
138
138
return this ;
139
139
}
140
140
141
+ private SchemaLoaderBuilder formatValidators (final Map <String , FormatValidator > formatValidators ) {
142
+ this .formatValidators = formatValidators ;
143
+ return this ;
144
+ }
145
+
141
146
}
142
147
143
148
private static final List <String > ARRAY_SCHEMA_PROPS = Arrays .asList ("items" , "additionalItems" ,
@@ -252,19 +257,19 @@ public SchemaLoader(final SchemaLoaderBuilder builder) {
252
257
253
258
private void addDependencies (final Builder builder , final JSONObject deps ) {
254
259
Arrays .stream (JSONObject .getNames (deps ))
255
- .forEach (ifPresent -> addDependency (builder , ifPresent , deps .get (ifPresent )));
260
+ .forEach (ifPresent -> addDependency (builder , ifPresent , deps .get (ifPresent )));
256
261
}
257
262
258
263
private void addDependency (final Builder builder , final String ifPresent , final Object deps ) {
259
264
typeMultiplexer (deps )
260
- .ifObject ().then (obj -> {
261
- builder .schemaDependency (ifPresent , loadChild (obj ).build ());
262
- })
263
- .ifIs (JSONArray .class ).then (propNames -> {
264
- IntStream .range (0 , propNames .length ())
265
- .mapToObj (i -> propNames .getString (i ))
266
- .forEach (dependency -> builder .propertyDependency (ifPresent , dependency ));
267
- }).requireAny ();
265
+ .ifObject ().then (obj -> {
266
+ builder .schemaDependency (ifPresent , loadChild (obj ).build ());
267
+ })
268
+ .ifIs (JSONArray .class ).then (propNames -> {
269
+ IntStream .range (0 , propNames .length ())
270
+ .mapToObj (i -> propNames .getString (i ))
271
+ .forEach (dependency -> builder .propertyDependency (ifPresent , dependency ));
272
+ }).requireAny ();
268
273
}
269
274
270
275
private void addFormatValidator (final StringSchema .Builder builder , final String formatName ) {
@@ -274,11 +279,11 @@ private void addFormatValidator(final StringSchema.Builder builder, final String
274
279
private void addPropertySchemaDefinition (final String keyOfObj , final Object definition ,
275
280
final ObjectSchema .Builder builder ) {
276
281
typeMultiplexer (definition )
277
- .ifObject ()
278
- .then (obj -> {
279
- builder .addPropertySchema (keyOfObj , loadChild (obj ).build ());
280
- })
281
- .requireAny ();
282
+ .ifObject ()
283
+ .then (obj -> {
284
+ builder .addPropertySchema (keyOfObj , loadChild (obj ).build ());
285
+ })
286
+ .requireAny ();
282
287
}
283
288
284
289
private CombinedSchema .Builder buildAnyOfSchemaForMultipleTypes () {
@@ -301,15 +306,15 @@ private ArraySchema.Builder buildArraySchema() {
301
306
ifPresent ("uniqueItems" , Boolean .class , builder ::uniqueItems );
302
307
if (schemaJson .has ("additionalItems" )) {
303
308
typeMultiplexer ("additionalItems" , schemaJson .get ("additionalItems" ))
304
- .ifIs (Boolean .class ).then (builder ::additionalItems )
305
- .ifObject ().then (jsonObj -> builder .schemaOfAdditionalItems (loadChild (jsonObj ).build ()))
306
- .requireAny ();
309
+ .ifIs (Boolean .class ).then (builder ::additionalItems )
310
+ .ifObject ().then (jsonObj -> builder .schemaOfAdditionalItems (loadChild (jsonObj ).build ()))
311
+ .requireAny ();
307
312
}
308
313
if (schemaJson .has ("items" )) {
309
314
typeMultiplexer ("items" , schemaJson .get ("items" ))
310
- .ifObject ().then (itemSchema -> builder .allItemSchema (loadChild (itemSchema ).build ()))
311
- .ifIs (JSONArray .class ).then (arr -> buildTupleSchema (builder , arr ))
312
- .requireAny ();
315
+ .ifObject ().then (itemSchema -> builder .allItemSchema (loadChild (itemSchema ).build ()))
316
+ .ifIs (JSONArray .class ).then (arr -> buildTupleSchema (builder , arr ))
317
+ .requireAny ();
313
318
}
314
319
return builder ;
315
320
}
@@ -318,8 +323,8 @@ private EnumSchema.Builder buildEnumSchema() {
318
323
Set <Object > possibleValues = new HashSet <>();
319
324
JSONArray arr = schemaJson .getJSONArray ("enum" );
320
325
IntStream .range (0 , arr .length ())
321
- .mapToObj (arr ::get )
322
- .forEach (possibleValues ::add );
326
+ .mapToObj (arr ::get )
327
+ .forEach (possibleValues ::add );
323
328
return EnumSchema .builder ().possibleValues (possibleValues );
324
329
}
325
330
@@ -344,21 +349,21 @@ private ObjectSchema.Builder buildObjectSchema() {
344
349
ifPresent ("maxProperties" , Integer .class , builder ::maxProperties );
345
350
if (schemaJson .has ("properties" )) {
346
351
typeMultiplexer (schemaJson .get ("properties" ))
347
- .ifObject ().then (propertyDefs -> {
348
- populatePropertySchemas (propertyDefs , builder );
349
- }).requireAny ();
352
+ .ifObject ().then (propertyDefs -> {
353
+ populatePropertySchemas (propertyDefs , builder );
354
+ }).requireAny ();
350
355
}
351
356
if (schemaJson .has ("additionalProperties" )) {
352
357
typeMultiplexer ("additionalProperties" , schemaJson .get ("additionalProperties" ))
353
- .ifIs (Boolean .class ).then (builder ::additionalProperties )
354
- .ifObject ().then (def -> builder .schemaOfAdditionalProperties (loadChild (def ).build ()))
355
- .requireAny ();
358
+ .ifIs (Boolean .class ).then (builder ::additionalProperties )
359
+ .ifObject ().then (def -> builder .schemaOfAdditionalProperties (loadChild (def ).build ()))
360
+ .requireAny ();
356
361
}
357
362
if (schemaJson .has ("required" )) {
358
363
JSONArray requiredJson = schemaJson .getJSONArray ("required" );
359
364
IntStream .range (0 , requiredJson .length ())
360
- .mapToObj (requiredJson ::getString )
361
- .forEach (builder ::addRequiredProperty );
365
+ .mapToObj (requiredJson ::getString )
366
+ .forEach (builder ::addRequiredProperty );
362
367
}
363
368
if (schemaJson .has ("patternProperties" )) {
364
369
JSONObject patternPropsJson = schemaJson .getJSONObject ("patternProperties" );
@@ -403,8 +408,8 @@ private StringSchema.Builder buildStringSchema() {
403
408
private void buildTupleSchema (final ArraySchema .Builder builder , final JSONArray itemSchema ) {
404
409
for (int i = 0 ; i < itemSchema .length (); ++i ) {
405
410
typeMultiplexer (itemSchema .get (i ))
406
- .ifObject ().then (schema -> builder .addItemSchema (loadChild (schema ).build ()))
407
- .requireAny ();
411
+ .ifObject ().then (schema -> builder .addItemSchema (loadChild (schema ).build ()))
412
+ .requireAny ();
408
413
}
409
414
}
410
415
@@ -518,16 +523,16 @@ private Schema.Builder<?> lookupReference(final String relPointerString, final J
518
523
}
519
524
JSONPointer pointer = absPointerString .startsWith ("#" )
520
525
? JSONPointer .forDocument (rootSchemaJson , absPointerString )
521
- : JSONPointer .forURL (httpClient , absPointerString );
522
- ReferenceSchema .Builder refBuilder = ReferenceSchema .builder ();
523
- pointerSchemas .put (absPointerString , refBuilder );
524
- QueryResult result = pointer .query ();
525
- JSONObject resultObject = extend (withoutRef (ctx ), result .getQueryResult ());
526
- SchemaLoader childLoader = selfBuilder ().schemaJson (resultObject )
527
- .rootSchemaJson (result .getContainingDocument ()).build ();
528
- Schema referredSchema = childLoader .load ().build ();
529
- refBuilder .build ().setReferredSchema (referredSchema );
530
- return refBuilder ;
526
+ : JSONPointer .forURL (httpClient , absPointerString );
527
+ ReferenceSchema .Builder refBuilder = ReferenceSchema .builder ();
528
+ pointerSchemas .put (absPointerString , refBuilder );
529
+ QueryResult result = pointer .query ();
530
+ JSONObject resultObject = extend (withoutRef (ctx ), result .getQueryResult ());
531
+ SchemaLoader childLoader = selfBuilder ().schemaJson (resultObject )
532
+ .rootSchemaJson (result .getContainingDocument ()).build ();
533
+ Schema referredSchema = childLoader .load ().build ();
534
+ refBuilder .build ().setReferredSchema (referredSchema );
535
+ return refBuilder ;
531
536
}
532
537
533
538
private void populatePropertySchemas (final JSONObject propertyDefs ,
@@ -546,10 +551,12 @@ private boolean schemaHasAnyOf(final Collection<String> propNames) {
546
551
}
547
552
548
553
private SchemaLoaderBuilder selfBuilder () {
549
- return builder ().id (id ).schemaJson (schemaJson )
554
+ SchemaLoaderBuilder rval = builder ().id (id ).schemaJson (schemaJson )
550
555
.rootSchemaJson (rootSchemaJson )
551
556
.pointerSchemas (pointerSchemas )
552
- .httpClient (httpClient );
557
+ .httpClient (httpClient )
558
+ .formatValidators (this .formatValidators );
559
+ return rval ;
553
560
}
554
561
555
562
private Schema .Builder <?> sniffSchemaByProps () {
@@ -621,8 +628,8 @@ JSONObject withoutRef(final JSONObject original) {
621
628
}
622
629
JSONObject rval = new JSONObject ();
623
630
Arrays .stream (names )
624
- .filter (name -> !"$ref" .equals (name ))
625
- .forEach (name -> rval .put (name , original .get (name )));
631
+ .filter (name -> !"$ref" .equals (name ))
632
+ .forEach (name -> rval .put (name , original .get (name )));
626
633
return rval ;
627
634
}
628
635
}
0 commit comments