OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.62k stars 6.29k forks source link

[BUG] Kotlin array items cannot be nullable #19070

Closed Blueblood1 closed 1 day ago

Blueblood1 commented 5 days ago

Bug Report Checklist

Description

In the Kotlin codegen a string array cannot be nullable.

openapi-generator version

Latest: 7.7.0

Not a regression.

OpenAPI declaration
ArrayWithNullableItemsModel:
  required:
    - foo
  properties:
    foo:
      type: array
      items:
        type: string
        nullable: true

Expected:

/**
 *
 * @param foo
 */
data class ArrayWithNullableItemsModel(

    @Schema(example = "null", required = true, description = "")
    @get:JsonProperty("foo", required = true) val foo: kotlin.collections.List<kotlin.String?>
) {

}

Actual:

/**
 *
 * @param foo
 */
data class ArrayWithNullableItemsModel(

    @Schema(example = "null", required = true, description = "")
    @get:JsonProperty("foo", required = true) val foo: kotlin.collections.List<kotlin.String>
) {

}
Generation Details
Steps to reproduce

See above.

Related issues/PRs

None

Suggest a fix
diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
index d97a242dbd4..7df7f8021c9 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java
@@ -347,6 +347,12 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
         return toModelName(type);
     }

+    private String getItemsTypeDeclaration(Schema items) {
+        String itemsTypeDeclaration = getTypeDeclaration(items);
+        String nullable = items.getNullable() != null && items.getNullable() && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
+        return itemsTypeDeclaration + nullable;
+    }
+
     /**
      * Output the type declaration of the property
      *
@@ -359,7 +365,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
         Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
         if (ModelUtils.isArraySchema(target)) {
             Schema<?> items = ModelUtils.getSchemaItems( schema);
-            return getSchemaType(target) + "<" + getTypeDeclaration(items) + ">";
+            return getSchemaType(target) + "<" + getItemsTypeDeclaration(items) + ">";
         } else if (ModelUtils.isMapSchema(target)) {
             // Note: ModelUtils.isMapSchema(p) returns true when p is a composed schema that also defines
             // additionalproperties: true
wing328 commented 4 days ago

can you please file a PR with the suggested fix when you've time?