@@ -350,13 +350,16 @@ class OpenAPIModelRegistrationSpec extends AnyFlatSpec {
350
350
)
351
351
}
352
352
353
- it should " add discriminator to sealed types and corresponding discriminator property to their children " +
353
+ it should " add discriminator to sealed types " +
354
+ " and discriminator property to all children (if addDiscriminatorPropertyOnlyToDirectChildren is false)" +
354
355
" if SumADTsShape is WithDiscriminator (with default DiscriminatorPropertyNameFn) in the config" in {
355
356
val components = new Components
356
357
val openAPIModelRegistration = new OpenAPIModelRegistration (
357
358
components,
358
359
config = OpenAPIModelRegistration .RegistrationConfig (
359
- sumADTsShape = OpenAPIModelRegistration .RegistrationConfig .SumADTsShape .WithDiscriminator ()
360
+ sumADTsShape = OpenAPIModelRegistration .RegistrationConfig .SumADTsShape .WithDiscriminator (
361
+ addDiscriminatorPropertyOnlyToDirectChildren = false
362
+ )
360
363
)
361
364
)
362
365
@@ -497,6 +500,146 @@ class OpenAPIModelRegistrationSpec extends AnyFlatSpec {
497
500
)
498
501
}
499
502
503
+ it should " add discriminator to sealed types " +
504
+ " and discriminator property to direct children (if addDiscriminatorPropertyOnlyToDirectChildren is true)" +
505
+ " if SumADTsShape is WithDiscriminator (with default DiscriminatorPropertyNameFn) in the config" in {
506
+ val components = new Components
507
+ val openAPIModelRegistration = new OpenAPIModelRegistration (
508
+ components,
509
+ config = OpenAPIModelRegistration .RegistrationConfig (
510
+ sumADTsShape = OpenAPIModelRegistration .RegistrationConfig .SumADTsShape .WithDiscriminator (
511
+ addDiscriminatorPropertyOnlyToDirectChildren = true
512
+ )
513
+ )
514
+ )
515
+
516
+ openAPIModelRegistration.register[NestedSealedTrait ]()
517
+
518
+ val actualSchemas = components.getSchemas
519
+
520
+ assertPredicateForPath(
521
+ actualSchemas,
522
+ " NestedSealedTrait" ,
523
+ schema => {
524
+ val actualDiscriminator = schema.getDiscriminator
525
+ val actualOneOf = schema.getOneOf.asScala
526
+ val expectedOneOf = Seq (
527
+ new Schema ().$ref(" #/components/schemas/NestedSealedTraitVariant1" ),
528
+ new Schema ().$ref(" #/components/schemas/NestedSealedTraitVariant2" ),
529
+ new Schema ().$ref(" #/components/schemas/NestedSealedTraitVariant3" )
530
+ )
531
+
532
+ val isPropertyNameCorrect = actualDiscriminator.getPropertyName === " nestedSealedTraitType"
533
+ val isMappingEmpty = Option (actualDiscriminator.getMapping).map(_.isEmpty).getOrElse(true )
534
+ val isOneOfCorrect = actualOneOf === expectedOneOf
535
+
536
+ isPropertyNameCorrect && isMappingEmpty && isOneOfCorrect
537
+ }
538
+ )
539
+
540
+ assertPredicateForPath(
541
+ actualSchemas,
542
+ " NestedSealedTraitVariant1" ,
543
+ schema => {
544
+ val actualProperties = schema.getProperties.asScala
545
+
546
+ val areNonDiscriminatorPropertiesCorrect = actualProperties.contains(" a" ) && actualProperties.contains(" b" )
547
+ val isDiscriminatorPropertyCorrect = actualProperties.contains(" nestedSealedTraitType" ) && {
548
+ val s = actualProperties(" nestedSealedTraitType" )
549
+ s.getType === " string" && s.getEnum.asScala === Seq (" NestedSealedTraitVariant1" )
550
+ }
551
+ val isDiscriminatorPropertyRequired = schema.getRequired.contains(" nestedSealedTraitType" )
552
+ val isCountOfPropertiesCorrect = actualProperties.size === 3
553
+
554
+ areNonDiscriminatorPropertiesCorrect &&
555
+ isDiscriminatorPropertyCorrect &&
556
+ isDiscriminatorPropertyRequired &&
557
+ isCountOfPropertiesCorrect
558
+ }
559
+ )
560
+
561
+ assertPredicateForPath(
562
+ actualSchemas,
563
+ " NestedSealedTraitVariant2" ,
564
+ schema => {
565
+ val actualProperties = schema.getProperties.asScala
566
+
567
+ val isDiscriminatorPropertyCorrect = actualProperties.contains(" nestedSealedTraitType" ) && {
568
+ val s = actualProperties(" nestedSealedTraitType" )
569
+ s.getType === " string" && s.getEnum.asScala === Seq (" NestedSealedTraitVariant2" )
570
+ }
571
+ val isDiscriminatorPropertyRequired = schema.getRequired.contains(" nestedSealedTraitType" )
572
+ val isCountOfPropertiesCorrect = actualProperties.size === 1
573
+
574
+ isDiscriminatorPropertyCorrect && isDiscriminatorPropertyRequired && isCountOfPropertiesCorrect
575
+ }
576
+ )
577
+
578
+ assertPredicateForPath(
579
+ actualSchemas,
580
+ " NestedSealedTraitVariant3" ,
581
+ schema => {
582
+ val actualDiscriminator = schema.getDiscriminator
583
+ val actualOneOf = schema.getOneOf.asScala
584
+ val expectedOneOf = Seq (
585
+ new Schema ().$ref(" #/components/schemas/NestedSealedTraitVariant3Subvariant1" ),
586
+ new Schema ().$ref(" #/components/schemas/NestedSealedTraitVariant3Subvariant2" )
587
+ )
588
+
589
+ val isPropertyNameCorrect = actualDiscriminator.getPropertyName === " nestedSealedTraitVariant3Type"
590
+ val isMappingEmpty = Option (actualDiscriminator.getMapping).map(_.isEmpty).getOrElse(true )
591
+ val isOneOfCorrect = actualOneOf === expectedOneOf
592
+ val isParentDiscriminatorNotInProperties = Option (schema.getProperties).map(_.size == 0 ).getOrElse(true )
593
+
594
+ isPropertyNameCorrect && isMappingEmpty && isOneOfCorrect && isParentDiscriminatorNotInProperties
595
+ }
596
+ )
597
+
598
+ assertPredicateForPath(
599
+ actualSchemas,
600
+ " NestedSealedTraitVariant3Subvariant1" ,
601
+ schema => {
602
+ val actualProperties = schema.getProperties.asScala
603
+
604
+ val areNonDiscriminatorPropertiesCorrect = actualProperties.contains(" a" )
605
+ val isDiscriminatorPropertyCorrect = actualProperties.contains(" nestedSealedTraitVariant3Type" ) && {
606
+ val s = actualProperties(" nestedSealedTraitVariant3Type" )
607
+ s.getType === " string" && s.getEnum.asScala === Seq (" NestedSealedTraitVariant3Subvariant1" )
608
+ }
609
+ val isParentDiscriminatorNotInProperties = ! actualProperties.contains(" nestedSealedTraitType" )
610
+ val isDiscriminatorPropertyRequired = schema.getRequired.contains(" nestedSealedTraitVariant3Type" )
611
+ val isCountOfPropertiesCorrect = actualProperties.size === 2
612
+
613
+ areNonDiscriminatorPropertiesCorrect &&
614
+ isDiscriminatorPropertyCorrect &&
615
+ isParentDiscriminatorNotInProperties &&
616
+ isDiscriminatorPropertyRequired &&
617
+ isCountOfPropertiesCorrect
618
+ }
619
+ )
620
+
621
+ assertPredicateForPath(
622
+ actualSchemas,
623
+ " NestedSealedTraitVariant3Subvariant2" ,
624
+ schema => {
625
+ val actualProperties = schema.getProperties.asScala
626
+
627
+ val isDiscriminatorPropertyCorrect = actualProperties.contains(" nestedSealedTraitVariant3Type" ) && {
628
+ val s = actualProperties(" nestedSealedTraitVariant3Type" )
629
+ s.getType === " string" && s.getEnum.asScala === Seq (" NestedSealedTraitVariant3Subvariant2" )
630
+ }
631
+ val isParentDiscriminatorNotInProperties = ! actualProperties.contains(" nestedSealedTraitType" )
632
+ val isDiscriminatorPropertyRequired = schema.getRequired.contains(" nestedSealedTraitVariant3Type" )
633
+ val isCountOfPropertiesCorrect = actualProperties.size === 1
634
+
635
+ isDiscriminatorPropertyCorrect &&
636
+ isParentDiscriminatorNotInProperties &&
637
+ isCountOfPropertiesCorrect &&
638
+ isDiscriminatorPropertyRequired
639
+ }
640
+ )
641
+ }
642
+
500
643
it should " not fail for empty sealed trait" in {
501
644
val components = new Components
502
645
val openAPIModelRegistration = new OpenAPIModelRegistration (components)
0 commit comments