Skip to content

Commit

Permalink
Move postProcessDataTypeWithEnum in SharedCodegen and apply it to all…
Browse files Browse the repository at this point in the history
… CodegenModel vars attributes
  • Loading branch information
macisamuele committed Feb 7, 2020
1 parent 27fd948 commit 11f0a6e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,35 +224,6 @@ open class KotlinGenerator : SharedCodegen() {
}
}

override fun postProcessModelProperty(model: CodegenModel, property: CodegenProperty) {
super.postProcessModelProperty(model, property)

if (property.isEnum) {
property.datatypeWithEnum = postProcessDataTypeWithEnum(model.classname, property)
}
}

/**
* When handling inner enums, we want to prefix their class name, when using them, with their containing class,
* to avoid name conflicts.
*/
private fun postProcessDataTypeWithEnum(modelClassName: String, codegenProperty: CodegenProperty): String {
val name = "$modelClassName.${codegenProperty.enumName}"

val baseType = if (codegenProperty.isContainer) {
val type = checkNotNull(typeMapping[codegenProperty.containerType])
"$type<$name>"
} else {
name
}

return if (codegenProperty.isNullable()) {
nullableTypeWrapper(baseType)
} else {
baseType
}
}

/**
* Returns the swagger type for the property
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
}

handleXNullable(codegenModel)

// Update all enum properties datatypeWithEnum to use "BaseClass.InnerEnumClass" to reduce ambiguity
CodegenModelVar.forEachVarAttribute(codegenModel) { _, properties ->
properties.forEach {
if (it.isEnum) {
it.datatypeWithEnum = this.postProcessDataTypeWithEnum(codegenModel, it)
}
}
}

return codegenModel
}

Expand Down Expand Up @@ -587,4 +597,33 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
* Nullable type are either not required or x-nullable annotated properties.
*/
internal fun CodegenProperty.isNullable() = !this.required || this.vendorExtensions[X_NULLABLE] == true

override fun postProcessModelProperty(model: CodegenModel, property: CodegenProperty) {
super.postProcessModelProperty(model, property)

if (property.isEnum) {
property.datatypeWithEnum = this.postProcessDataTypeWithEnum(model, property)
}
}

/**
* When handling inner enums, we want to prefix their class name, when using them, with their containing class,
* to avoid name conflicts.
*/
private fun postProcessDataTypeWithEnum(codegenModel: CodegenModel, codegenProperty: CodegenProperty): String {
val name = "${codegenModel.classname}.${codegenProperty.enumName}"

val baseType = if (codegenProperty.isContainer) {
val type = checkNotNull(typeMapping[codegenProperty.containerType])
"$type<$name>"
} else {
name
}

return if (codegenProperty.isNullable()) {
nullableTypeWrapper(baseType)
} else {
baseType
}
}
}

0 comments on commit 11f0a6e

Please sign in to comment.