Skip to content

Commit

Permalink
🐛 Fix nested array type
Browse files Browse the repository at this point in the history
  • Loading branch information
David Sucharda committed Apr 24, 2020
1 parent aa7be51 commit 718585d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

## 2.1.2 (TBD)

### Fixed
- Nested arrays and maps in arrays contain type. Array<Array>> is now properly generated as Array<Array<Type>>.

## 2.1.1 (2020-04-22)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
org.gradle.jvmargs=-Xmx1536m

version=2.1.1
version=2.1.2
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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].
Expand Down

0 comments on commit 718585d

Please sign in to comment.