@@ -1240,7 +1240,7 @@ public String toArrayDefaultValue(CodegenProperty cp, Schema schema) {
12401240 if (schema .getDefault () instanceof ArrayNode ) { // array of default values
12411241 ArrayNode _default = (ArrayNode ) schema .getDefault ();
12421242 if (_default .isEmpty ()) { // e.g. default: []
1243- return getDefaultCollectionType (schema );
1243+ return getDefaultCollectionType (schema , "" );
12441244 }
12451245
12461246 List <String > final_values = _values ;
@@ -1253,6 +1253,11 @@ public String toArrayDefaultValue(CodegenProperty cp, Schema schema) {
12531253 _default .forEach ((element ) -> {
12541254 final_values .add (String .valueOf (element ));
12551255 });
1256+
1257+ if (_default != null && _default .isEmpty () && defaultToEmptyContainer ) {
1258+ // e.g. [] with the option defaultToEmptyContainer enabled
1259+ return getDefaultCollectionType (schema , "" );
1260+ }
12561261 } else { // single value
12571262 _values = java .util .Collections .singletonList (String .valueOf (schema .getDefault ()));
12581263 }
@@ -1431,12 +1436,31 @@ private String getDefaultCollectionType(Schema schema) {
14311436
14321437 private String getDefaultCollectionType (Schema schema , String defaultValues ) {
14331438 String arrayFormat = "new %s<>(Arrays.asList(%s))" ;
1439+
1440+ if (defaultToEmptyContainer ) {
1441+ // respect the default value in the spec
1442+ if (defaultValues == null ) { // default value not provided
1443+ return null ;
1444+ } else if (defaultValues .isEmpty ()) { // e.g. [] to indicates empty container
1445+ arrayFormat = "new %s<>()" ;
1446+ return getDefaultCollectionType (arrayFormat , defaultValues , ModelUtils .isSet (schema ));
1447+ } else { // default value not empty
1448+ return getDefaultCollectionType (arrayFormat , defaultValues , ModelUtils .isSet (schema ));
1449+ }
1450+ }
1451+
14341452 if (defaultValues == null || defaultValues .isEmpty ()) {
1453+ // default to empty container even though default value is null
1454+ // to respect default values provided in the spec, set the option `defaultToEmptyContainer` properly
14351455 defaultValues = "" ;
14361456 arrayFormat = "new %s<>()" ;
14371457 }
14381458
1439- if (ModelUtils .isSet (schema )) {
1459+ return getDefaultCollectionType (arrayFormat , defaultValues , ModelUtils .isSet (schema ));
1460+ }
1461+
1462+ private String getDefaultCollectionType (String arrayFormat , String defaultValues , boolean isSet ) {
1463+ if (isSet ) {
14401464 return String .format (Locale .ROOT , arrayFormat ,
14411465 instantiationTypes ().getOrDefault ("set" , "LinkedHashSet" ), defaultValues );
14421466 }
0 commit comments