Skip to content

Commit 7a2369f

Browse files
committed
Move postProcessDataTypeWithEnum in SharedCodegen and apply it to all CodegenModel vars attributes
1 parent 398dc23 commit 7a2369f

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

gradle-plugin/plugin/src/main/java/com/yelp/codegen/KotlinGenerator.kt

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -224,35 +224,6 @@ open class KotlinGenerator : SharedCodegen() {
224224
}
225225
}
226226

227-
override fun postProcessModelProperty(model: CodegenModel, property: CodegenProperty) {
228-
super.postProcessModelProperty(model, property)
229-
230-
if (property.isEnum) {
231-
property.datatypeWithEnum = postProcessDataTypeWithEnum(model.classname, property)
232-
}
233-
}
234-
235-
/**
236-
* When handling inner enums, we want to prefix their class name, when using them, with their containing class,
237-
* to avoid name conflicts.
238-
*/
239-
private fun postProcessDataTypeWithEnum(modelClassName: String, codegenProperty: CodegenProperty): String {
240-
val name = "$modelClassName.${codegenProperty.enumName}"
241-
242-
val baseType = if (codegenProperty.isContainer) {
243-
val type = checkNotNull(typeMapping[codegenProperty.containerType])
244-
"$type<$name>"
245-
} else {
246-
name
247-
}
248-
249-
return if (codegenProperty.isNullable()) {
250-
nullableTypeWrapper(baseType)
251-
} else {
252-
baseType
253-
}
254-
}
255-
256227
/**
257228
* Returns the swagger type for the property
258229
*

gradle-plugin/plugin/src/main/java/com/yelp/codegen/SharedCodegen.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
259259
}
260260

261261
handleXNullable(codegenModel)
262+
263+
// Update all enum properties datatypeWithEnum to use "BaseClass.InnerEnumClass" to reduce ambiguity
264+
CodegenModelVar.forEachVarAttribute(codegenModel) { _, properties ->
265+
properties.forEach {
266+
if (it.isEnum) {
267+
it.datatypeWithEnum = this.postProcessDataTypeWithEnum(codegenModel, it)
268+
}
269+
}
270+
}
271+
262272
return codegenModel
263273
}
264274

@@ -590,4 +600,33 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
590600
* Nullable type are either not required or x-nullable annotated properties.
591601
*/
592602
internal fun CodegenProperty.isNullable() = !this.required || this.vendorExtensions[X_NULLABLE] == true
603+
604+
override fun postProcessModelProperty(model: CodegenModel, property: CodegenProperty) {
605+
super.postProcessModelProperty(model, property)
606+
607+
if (property.isEnum) {
608+
property.datatypeWithEnum = this.postProcessDataTypeWithEnum(model, property)
609+
}
610+
}
611+
612+
/**
613+
* When handling inner enums, we want to prefix their class name, when using them, with their containing class,
614+
* to avoid name conflicts.
615+
*/
616+
private fun postProcessDataTypeWithEnum(codegenModel: CodegenModel, codegenProperty: CodegenProperty): String {
617+
val name = "${codegenModel.classname}.${codegenProperty.enumName}"
618+
619+
val baseType = if (codegenProperty.isContainer) {
620+
val type = checkNotNull(typeMapping[codegenProperty.containerType])
621+
"$type<$name>"
622+
} else {
623+
name
624+
}
625+
626+
return if (codegenProperty.isNullable()) {
627+
nullableTypeWrapper(baseType)
628+
} else {
629+
baseType
630+
}
631+
}
593632
}

0 commit comments

Comments
 (0)