Closed Blueblood1 closed 1 day ago
In the Kotlin codegen a string array cannot be nullable.
Latest: 7.7.0
Not a regression.
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> ) { }
See above.
None
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
can you please file a PR with the suggested fix when you've time?
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
Expected:
Actual:
Generation Details
Steps to reproduce
See above.
Related issues/PRs
None
Suggest a fix