Skip to content

Commit 11f0a6e

Browse files
committed
Move postProcessDataTypeWithEnum in SharedCodegen and apply it to all CodegenModel vars attributes
1 parent 27fd948 commit 11f0a6e

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
@@ -256,6 +256,16 @@ abstract class SharedCodegen : DefaultCodegen(), CodegenConfig {
256256
}
257257

258258
handleXNullable(codegenModel)
259+
260+
// Update all enum properties datatypeWithEnum to use "BaseClass.InnerEnumClass" to reduce ambiguity
261+
CodegenModelVar.forEachVarAttribute(codegenModel) { _, properties ->
262+
properties.forEach {
263+
if (it.isEnum) {
264+
it.datatypeWithEnum = this.postProcessDataTypeWithEnum(codegenModel, it)
265+
}
266+
}
267+
}
268+
259269
return codegenModel
260270
}
261271

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

0 commit comments

Comments
 (0)