@@ -80,7 +80,15 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest {
80
80
81
81
then(lineItemSchema.allItemSchema).isInstanceOf(ObjectSchema ::class .java)
82
82
83
- thenSchemaIsValid()
83
+ then(objectSchema.propertySchemas[" lineItems" ]).isInstanceOf(ArraySchema ::class .java)
84
+
85
+ val paymentLineItem = objectSchema.propertySchemas[" paymentLineItem" ] as ObjectSchema
86
+
87
+ val lineItemsTaxesSchema = paymentLineItem.propertySchemas[" lineItemTaxes" ] as ArraySchema
88
+ then(lineItemsTaxesSchema.minItems).isEqualTo(1 )
89
+ then(lineItemsTaxesSchema.maxItems).isEqualTo(255 )
90
+ then(lineItemsTaxesSchema.requiresArray()).isTrue()
91
+
84
92
// language=JSON
85
93
thenSchemaValidatesJson(
86
94
""" {
@@ -168,6 +176,18 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest {
168
176
then(objSchema.requiredProperties).contains(" obj" )
169
177
}
170
178
179
+ @Test
180
+ fun should_generate_schema_for_required_array_in_object () {
181
+ givenFieldDescriptorWithRequiredArray()
182
+
183
+ whenSchemaGenerated()
184
+
185
+ then(schema).isInstanceOf(ObjectSchema ::class .java)
186
+ thenSchemaIsValid()
187
+ val objSchema = schema!! .let { it as ObjectSchema }
188
+ then(objSchema.requiredProperties).contains(" obj" )
189
+ }
190
+
171
191
@Test
172
192
fun should_fail_on_unknown_field_type () {
173
193
givenFieldDescriptorWithInvalidType()
@@ -230,6 +250,45 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest {
230
250
thenSchemaValidatesJson(""" { some: "ENUM_VALUE_1" }""" )
231
251
}
232
252
253
+ @Test
254
+ fun should_generate_schema_for_top_level_array_with_size_constraint () {
255
+ givenFieldDescriptorWithTopLevelArrayWithSizeConstraint()
256
+
257
+ whenSchemaGenerated()
258
+
259
+ then(schema).isInstanceOf(ArraySchema ::class .java)
260
+ then((schema as ArraySchema ).minItems).isEqualTo(1 )
261
+ then((schema as ArraySchema ).maxItems).isEqualTo(255 )
262
+ thenSchemaIsValid()
263
+ }
264
+
265
+ @Test
266
+ fun should_generate_schema_for_top_level_array_of_arrays_with_size_constraint () {
267
+ givenFieldDescriptorWithTopLevelArrayOfArraysWithSizeConstraint()
268
+
269
+ whenSchemaGenerated()
270
+
271
+ then(schema).isInstanceOf(ArraySchema ::class .java)
272
+ then((schema as ArraySchema ).allItemSchema).isInstanceOf(ArraySchema ::class .java)
273
+ then(((schema as ArraySchema ).allItemSchema as ArraySchema ).minItems).isEqualTo(1 )
274
+ then(((schema as ArraySchema ).allItemSchema as ArraySchema ).maxItems).isEqualTo(255 )
275
+ thenSchemaIsValid()
276
+ }
277
+
278
+ @Test
279
+ fun should_generate_schema_for_array_with_size_constraint () {
280
+ givenFieldDescriptorUnspecifiedArrayItemsWithSizeConstraint()
281
+
282
+ whenSchemaGenerated()
283
+
284
+ then(schema).isInstanceOf(ObjectSchema ::class .java)
285
+ then((schema as ObjectSchema ).definesProperty(" some" )).isTrue
286
+ then((schema as ObjectSchema ).propertySchemas[" some" ]).isInstanceOf(ArraySchema ::class .java)
287
+ then(((schema as ObjectSchema ).propertySchemas[" some" ] as ArraySchema ).minItems).isEqualTo(1 )
288
+ then(((schema as ObjectSchema ).propertySchemas[" some" ] as ArraySchema ).maxItems).isEqualTo(255 )
289
+ thenSchemaIsValid()
290
+ }
291
+
233
292
private fun thenSchemaIsValid () {
234
293
235
294
val report = JsonSchemaFactory .byDefault()
@@ -256,6 +315,14 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest {
256
315
)
257
316
}
258
317
318
+ private fun givenFieldDescriptorWithRequiredArray () {
319
+ val notNullConstraint = Attributes (listOf (Constraint (NotNull ::class .java.name, emptyMap())))
320
+ fieldDescriptors = listOf (
321
+ FieldDescriptor (" array" , " someArray" , " ARRAY" , attributes = notNullConstraint),
322
+ FieldDescriptor (" array[].field" , " some" , " STRING" )
323
+ )
324
+ }
325
+
259
326
private fun givenFieldDescriptorWithTopLevelArray () {
260
327
fieldDescriptors = listOf (FieldDescriptor (" []['id']" , " some" , " STRING" ))
261
328
}
@@ -272,6 +339,50 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest {
272
339
fieldDescriptors = listOf (FieldDescriptor (" some[]" , " some" , " ARRAY" ))
273
340
}
274
341
342
+ private fun givenFieldDescriptorWithTopLevelArrayWithSizeConstraint () {
343
+ fieldDescriptors = listOf (
344
+ FieldDescriptor (
345
+ " []" ,
346
+ " some" ,
347
+ " ARRAY" ,
348
+ attributes = Attributes (
349
+ listOf (
350
+ Constraint (
351
+ " javax.validation.constraints.Size" ,
352
+ mapOf (" min" to 1 , " max" to 255 )
353
+ )
354
+ )
355
+ )
356
+ )
357
+ )
358
+ }
359
+
360
+ private fun givenFieldDescriptorWithTopLevelArrayOfArraysWithSizeConstraint () {
361
+ fieldDescriptors = listOf (
362
+ FieldDescriptor (
363
+ " [][]" ,
364
+ " some" ,
365
+ " ARRAY" ,
366
+ attributes = Attributes (
367
+ listOf (Constraint (" javax.validation.constraints.Size" , mapOf (" min" to 1 , " max" to 255 )))
368
+ )
369
+ )
370
+ )
371
+ }
372
+
373
+ private fun givenFieldDescriptorUnspecifiedArrayItemsWithSizeConstraint () {
374
+ fieldDescriptors = listOf (
375
+ FieldDescriptor (
376
+ " some[]" ,
377
+ " some" ,
378
+ " ARRAY" ,
379
+ attributes = Attributes (
380
+ listOf (Constraint (" javax.validation.constraints.Size" , mapOf (" min" to 1 , " max" to 255 )))
381
+ )
382
+ )
383
+ )
384
+ }
385
+
275
386
private fun givenFieldDescriptorWithInvalidType () {
276
387
fieldDescriptors = listOf (FieldDescriptor (" id" , " some" , " invalid-type" ))
277
388
}
@@ -340,6 +451,7 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest {
340
451
" NUMBER" ,
341
452
attributes = constraintAttributeWithNotNull
342
453
),
454
+
343
455
FieldDescriptor (" lineItems[*].quantity.unit" , " some" , " STRING" ),
344
456
FieldDescriptor (" shippingAddress" , " some" , " OBJECT" ),
345
457
FieldDescriptor (" billingAddress" , " some" , " OBJECT" ),
@@ -355,7 +467,26 @@ class JsonSchemaFromFieldDescriptorsGeneratorTest {
355
467
)
356
468
),
357
469
FieldDescriptor (" billingAddress.valid" , " some" , " BOOLEAN" ),
358
- FieldDescriptor (" paymentLineItem.lineItemTaxes" , " some" , " ARRAY" )
470
+ FieldDescriptor (
471
+ " paymentLineItem.lineItemTaxes" ,
472
+ " some" ,
473
+ " ARRAY" ,
474
+ attributes = Attributes (
475
+ listOf (
476
+ Constraint (
477
+ " javax.validation.constraints.Size" ,
478
+ mapOf (
479
+ " min" to 1 ,
480
+ " max" to 255
481
+ )
482
+ ),
483
+ Constraint (
484
+ NotNull ::class .java.name,
485
+ emptyMap()
486
+ )
487
+ )
488
+ )
489
+ )
359
490
)
360
491
}
361
492
0 commit comments