24
24
import java .time .OffsetDateTime ;
25
25
import java .time .ZoneId ;
26
26
import java .time .format .DateTimeFormatter ;
27
- import java .util .ArrayList ;
28
- import java .util .Arrays ;
29
- import java .util .Base64 ;
30
- import java .util .Collection ;
31
- import java .util .Collections ;
32
- import java .util .Date ;
33
- import java .util .HashMap ;
34
- import java .util .List ;
35
- import java .util .Locale ;
36
- import java .util .Map ;
37
- import java .util .Objects ;
38
- import java .util .Optional ;
39
- import java .util .Random ;
40
- import java .util .Set ;
27
+ import java .util .*;
41
28
import java .util .function .Supplier ;
42
29
import java .util .stream .Collectors ;
43
30
@@ -545,10 +532,7 @@ private List<Object> resolvePropertyToExamples(String propertyName, Schema prope
545
532
recordRequestSchema (currentProperty , property );
546
533
547
534
if (CatsModelUtils .isArraySchema (property )) {
548
- Schema itemSchema = CatsModelUtils .getSchemaItems (property );
549
- List <Object > itemExamples = resolvePropertyToExamples (propertyName + ".items" , itemSchema );
550
- int arraySize = getArrayLength (property );
551
- examples .addAll (itemExamples .stream ().map (innerExample -> Collections .nCopies (arraySize , innerExample )).toList ());
535
+ createExamplesArray (propertyName , property , examples );
552
536
} else if (CatsModelUtils .isMapSchema (property )) {
553
537
Map <String , Object > mapExample = new HashMap <>();
554
538
Schema additionalProperties = CatsModelUtils .getAdditionalProperties (property );
@@ -585,6 +569,25 @@ private List<Object> resolvePropertyToExamples(String propertyName, Schema prope
585
569
return examples ;
586
570
}
587
571
572
+ private void createExamplesArray (String propertyName , Schema property , List <Object > examples ) {
573
+ if (Boolean .TRUE .equals (property .getUniqueItems ())) {
574
+ logger .trace ("Creating unique items array for property {}" , propertyName );
575
+ int arraySize = getArrayLength (property );
576
+ Schema itemSchema = CatsModelUtils .getSchemaItems (property );
577
+ Set <Object > itemExamples = new HashSet <>();
578
+ while (itemExamples .size () < arraySize ) {
579
+ Object itemExample = resolvePropertyToExample (propertyName + ".items" , itemSchema , false );
580
+ itemExamples .add (itemExample );
581
+ }
582
+ examples .add (itemExamples );
583
+ } else {
584
+ Schema itemSchema = CatsModelUtils .getSchemaItems (property );
585
+ List <Object > itemExamples = resolvePropertyToExamples (propertyName + ".items" , itemSchema );
586
+ int arraySize = getArrayLength (property );
587
+ examples .addAll (itemExamples .stream ().map (innerExample -> Collections .nCopies (arraySize , innerExample )).toList ());
588
+ }
589
+ }
590
+
588
591
private void mapDiscriminator (Schema <?> composedSchema , List <Schema > anyOf ) {
589
592
if (composedSchema .getDiscriminator () != null ) {
590
593
logger .trace ("Mapping discriminator for schema {}" , composedSchema .getName ());
@@ -731,8 +734,17 @@ private <T> Object generateStringValue(String propertyName, Schema<T> propertySc
731
734
732
735
private <T > Object getEnumOrDefault (Schema <T > propertySchema ) {
733
736
List <T > enumValues = propertySchema .getEnum ();
737
+
734
738
if (!CollectionUtils .isEmpty (enumValues )) {
735
- return enumValues .stream ().filter (Objects ::nonNull ).findAny ().orElse (enumValues .getFirst ());
739
+ List <T > nonNullEnumValues = enumValues .stream ()
740
+ .filter (Objects ::nonNull )
741
+ .toList ();
742
+
743
+ if (!nonNullEnumValues .isEmpty ()) {
744
+ return nonNullEnumValues .get (CatsUtil .random ().nextInt (nonNullEnumValues .size ()));
745
+ }
746
+
747
+ return enumValues .getFirst (); // fallback if all were null
736
748
}
737
749
738
750
if (propertySchema .getDefault () != null && useDefaults ) {
@@ -791,10 +803,10 @@ private List<Map<String, Object>> combineExampleLists(List<Map<String, Object>>
791
803
return combined ;
792
804
}
793
805
794
- private Object resolvePropertyToExample (String propertyName , Schema propertySchema ) {
806
+ private Object resolvePropertyToExample (String propertyName , Schema propertySchema , boolean useExamples ) {
795
807
logger .trace ("resolvePropertyToExample for property {}" , propertyName );
796
808
//examples will take first priority
797
- Object example = this .extractExampleFromSchema (propertySchema , examplesFlags . usePropertyExamples () );
809
+ Object example = this .extractExampleFromSchema (propertySchema , useExamples );
798
810
if (example != null ) {
799
811
logger .trace ("Example set in swagger spec, returning example: '{}'" , example );
800
812
return example ;
@@ -813,6 +825,10 @@ private Object resolvePropertyToExample(String propertyName, Schema propertySche
813
825
return generateExampleBySchemaType (propertyName , propertySchema );
814
826
}
815
827
828
+ private Object resolvePropertyToExample (String propertyName , Schema propertySchema ) {
829
+ return resolvePropertyToExample (propertyName , propertySchema , examplesFlags .usePropertyExamples ());
830
+ }
831
+
816
832
private <T > Object generateExampleBySchemaType (String propertyName , Schema <T > propertySchema ) {
817
833
if (CatsModelUtils .isStringSchema (propertySchema )) {
818
834
return this .getExampleFromStringSchema (propertyName , propertySchema );
0 commit comments