From 718585d6cbbbd56e7261fc6e01e30ec8272e0e1c Mon Sep 17 00:00:00 2001 From: David Sucharda Date: Fri, 24 Apr 2020 17:30:01 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20nested=20array=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++ gradle.properties | 2 +- .../generator/kotlin/KotlinClientCodegen.kt | 31 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d626176..dd39b08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Change Log ========== +## 2.1.2 (TBD) + +### Fixed +- Nested arrays and maps in arrays contain type. Array> is now properly generated as Array>. + ## 2.1.1 (2020-04-22) ### Fixed diff --git a/gradle.properties b/gradle.properties index 12c9d4f..ee9616f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ org.gradle.jvmargs=-Xmx1536m -version=2.1.1 \ No newline at end of file +version=2.1.2 \ No newline at end of file diff --git a/lib/src/main/kotlin/cz/eman/swagger/codegen/generator/kotlin/KotlinClientCodegen.kt b/lib/src/main/kotlin/cz/eman/swagger/codegen/generator/kotlin/KotlinClientCodegen.kt index dd2c850..5fcd764 100644 --- a/lib/src/main/kotlin/cz/eman/swagger/codegen/generator/kotlin/KotlinClientCodegen.kt +++ b/lib/src/main/kotlin/cz/eman/swagger/codegen/generator/kotlin/KotlinClientCodegen.kt @@ -9,6 +9,7 @@ import io.swagger.v3.oas.models.media.* import org.gradle.util.CollectionUtils.sort import org.openapitools.codegen.* import org.openapitools.codegen.languages.AbstractKotlinCodegen +import org.openapitools.codegen.utils.ModelUtils import org.slf4j.Logger import org.slf4j.LoggerFactory import java.io.File @@ -143,9 +144,22 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient override fun fromModel(name: String?, schema: Schema<*>?): CodegenModel { emptyDataClassAsString(name, schema) composedArrayAsAny(name, schema) + fixArrayItemsSchema(name, schema) return super.fromModel(name, schema) } + /** + * Modifies property before actual [fromProperty] function is called. Fixes [ArraySchema] items to contain proper + * [ArraySchema] or [MapSchema] since it lost it's type and generated incorrect data type for nested arrays and + * maps. + * + * @since 2.1.2 + */ + override fun fromProperty(name: String?, schema: Schema<*>?): CodegenProperty { + fixArrayItemsSchema(name, schema) + return super.fromProperty(name, schema) + } + /** * Enum number names are unified with Java code generation. * @@ -512,6 +526,23 @@ open class KotlinClientCodegen : org.openapitools.codegen.languages.KotlinClient } } + /** + * Fixes [ArraySchema] items to contain proper [ArraySchema] or [MapSchema] since it lost it's type and generated + * incorrect data type for nested arrays and maps. + * + * @since 2.1.2 + */ + private fun fixArrayItemsSchema(name: String?, property: Schema<*>?) { + if(property is ArraySchema && property.items != null) { + logger.info("Trying to fix array items for: $name") + val itemsSchema = ModelUtils.getReferencedSchema(openAPI, property.items) + if(ModelUtils.isMapSchema(itemsSchema) || ModelUtils.isArraySchema(itemsSchema)) { + logger.info("Array items is Map or Array schema") + property.items = itemsSchema + } + } + } + /** * Sets vendor extensions to the model and it's properties. Extensions added: [markModelAsTypeAlias] and * [escapePropertyBaseNameLiteral].