@@ -334,6 +334,11 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
334334 // Whether to automatically hardcode params that are considered Constants by OpenAPI Spec
335335 @ Setter protected boolean autosetConstants = false ;
336336
337+ @ Setter @ Getter boolean arrayDefaultToEmpty , arrayNullableDefaultToEmpty , arrayOptionalNullableDefaultToEmpty , arrayOptionalDefaultToEmpty ;
338+ @ Setter @ Getter boolean mapDefaultToEmpty , mapNullableDefaultToEmpty , mapOptionalNullableDefaultToEmpty , mapOptionalDefaultToEmpty ;
339+ final String DEFAULT_TO_EMPTY_CONTAINER = "defaultToEmptyContainer" ;
340+ final List EMPTY_LIST = new ArrayList ();
341+
337342 @ Override
338343 public boolean getAddSuffixToDuplicateOperationNicknames () {
339344 return addSuffixToDuplicateOperationNicknames ;
@@ -392,8 +397,11 @@ public void processOpts() {
392397 convertPropertyToBooleanAndWriteBack (CodegenConstants .DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT , this ::setDisallowAdditionalPropertiesIfNotPresent );
393398 convertPropertyToBooleanAndWriteBack (CodegenConstants .ENUM_UNKNOWN_DEFAULT_CASE , this ::setEnumUnknownDefaultCase );
394399 convertPropertyToBooleanAndWriteBack (CodegenConstants .AUTOSET_CONSTANTS , this ::setAutosetConstants );
395- }
396400
401+ if (additionalProperties .containsKey (DEFAULT_TO_EMPTY_CONTAINER ) && additionalProperties .get (DEFAULT_TO_EMPTY_CONTAINER ) instanceof String ) {
402+ parseDefaultToEmptyContainer ((String ) additionalProperties .get (DEFAULT_TO_EMPTY_CONTAINER ));
403+ }
404+ }
397405
398406 /***
399407 * Preset map builder with commonly used Mustache lambdas.
@@ -4226,6 +4234,11 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
42264234 }
42274235 }
42284236
4237+ // override defaultValue if it's not set and defaultToEmptyContainer is set
4238+ if (p .getDefault () == null && additionalProperties .containsKey ("defaultToEmptyContainer" )) {
4239+ updateDefaultToEmptyContainer (property , p );
4240+ }
4241+
42294242 // set the default value
42304243 property .defaultValue = toDefaultValue (property , p );
42314244 property .defaultValueWithParam = toDefaultValueWithParam (name , p );
@@ -4235,6 +4248,90 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
42354248 return property ;
42364249 }
42374250
4251+ /**
4252+ * update container's default to empty container according rules provided by the user.
4253+ *
4254+ * @param cp codegen property
4255+ * @param p schema
4256+ */
4257+ void updateDefaultToEmptyContainer (CodegenProperty cp , Schema p ) {
4258+ if (cp .isArray ) {
4259+ if (!cp .required ) { // optional
4260+ if (cp .isNullable && arrayOptionalNullableDefaultToEmpty ) { // nullable
4261+ p .setDefault (EMPTY_LIST );
4262+ } else if (!cp .isNullable && arrayOptionalDefaultToEmpty ) { // non-nullable
4263+ p .setDefault (EMPTY_LIST );
4264+ }
4265+ } else { // required
4266+ if (cp .isNullable && arrayNullableDefaultToEmpty ) { // nullable
4267+ p .setDefault (EMPTY_LIST );
4268+ } else if (!cp .isNullable && arrayDefaultToEmpty ) { // non-nullable
4269+ p .setDefault (EMPTY_LIST );
4270+ }
4271+ }
4272+ } else if (cp .isMap ) {
4273+ if (!cp .required ) { // optional
4274+ if (cp .isNullable && mapOptionalNullableDefaultToEmpty ) { // nullable
4275+ p .setDefault (EMPTY_LIST );
4276+ } else if (!cp .isNullable && mapOptionalDefaultToEmpty ) { // non-nullable
4277+ p .setDefault (EMPTY_LIST );
4278+ }
4279+ } else { // required
4280+ if (cp .isNullable && mapNullableDefaultToEmpty ) { // nullable
4281+ p .setDefault (EMPTY_LIST );
4282+ } else if (!cp .isNullable && mapOptionalDefaultToEmpty ) { // non-nullable
4283+ p .setDefault (EMPTY_LIST );
4284+ }
4285+ }
4286+ }
4287+ }
4288+
4289+ /**
4290+ * Parse the rules for defaulting to the empty container.
4291+ *
4292+ * @param input a set of rules separated by `|`
4293+ */
4294+ void parseDefaultToEmptyContainer (String input ) {
4295+ String [] inputs = ((String ) input ).split ("[|]" );
4296+ String containerType ;
4297+ for (String rule : inputs ) {
4298+ if (StringUtils .isEmpty (rule )) {
4299+ LOGGER .error ("updateDefaultToEmptyContainer: Skipped empty input in `{}`." , input );
4300+ continue ;
4301+ }
4302+
4303+ if (rule .startsWith ("?" ) && rule .endsWith ("?" )) { // nullable optional
4304+ containerType = rule .substring (1 , rule .length () - 1 );
4305+ if ("array" .equalsIgnoreCase (containerType )) {
4306+ arrayOptionalNullableDefaultToEmpty = true ;
4307+ } else if ("map" .equalsIgnoreCase (containerType )) {
4308+ mapOptionalNullableDefaultToEmpty = true ;
4309+ } else {
4310+ LOGGER .error ("Skipped invalid container type `{}` in `{}`." , containerType , input );
4311+ }
4312+ } else if (rule .startsWith ("?" )) { // nullable (required)
4313+ containerType = rule .substring (1 , rule .length ());
4314+ if ("array" .equalsIgnoreCase (containerType )) {
4315+ arrayNullableDefaultToEmpty = true ;
4316+ } else if ("map" .equalsIgnoreCase (containerType )) {
4317+ mapNullableDefaultToEmpty = true ;
4318+ } else {
4319+ LOGGER .error ("Skipped invalid container type `{}` in `{}`." , containerType , input );
4320+ }
4321+ } else if (rule .endsWith ("?" )) { // optional
4322+ containerType = rule .substring (0 , rule .length ()-1 );
4323+ if ("array" .equalsIgnoreCase (containerType )) {
4324+ arrayOptionalDefaultToEmpty = true ;
4325+ } else if ("map" .equalsIgnoreCase (containerType )) {
4326+ mapOptionalDefaultToEmpty = true ;
4327+ } else {
4328+ LOGGER .error ("Skipped invalid container type `{}` in the rule `{}`." , containerType , input );
4329+ }
4330+ }
4331+ }
4332+
4333+ }
4334+
42384335 /**
42394336 * Update property for array(list) container
42404337 *
0 commit comments