Skip to content

Commit

Permalink
Properly handle parameterized mapped superclass issue #891
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmsilva committed Feb 11, 2025
1 parent c2f2467 commit 594f72a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.joinToCode
import kotlin.reflect.KClass

fun Type.asTypeName(): TypeName = asClassName().let { className ->
fun Type.asTypeName(out: Boolean = false): TypeName = asClassName().let { className ->
if (parameters.isNotEmpty())
className.parameterizedBy(*parameters.map { it.asTypeName() }.toTypedArray()) else className
className.parameterizedBy(*parameters.map { if (out) it.asOutTypeName() else it.asTypeName() }.toTypedArray()) else className
}

fun Type.asClassName(): ClassName = when (this.fullName) {
Expand Down Expand Up @@ -57,7 +57,7 @@ fun Type.asClassName(): ClassName = when (this.fullName) {
else -> ClassName(packageName, *enclosingTypeHierarchy().toTypedArray())
}

fun Type.asOutTypeName() = WildcardTypeName.producerOf(asTypeName())
fun Type.asOutTypeName() = WildcardTypeName.producerOf(asTypeName(out = true))

private fun Type.enclosingTypeHierarchy(): List<String> {
var current: Type? = this
Expand All @@ -72,10 +72,12 @@ fun TypeMappings.getPathClassName(type: Type, model: EntityType) = getPathType(t

fun TypeMappings.getPathTypeName(type: Type, model: EntityType) = getPathType(type, model, false).asTypeName()

fun KClass<*>.parameterizedBy(vararg types: TypeName) = asTypeName().parameterizedBy(*types)
fun KClass<*>.parameterizedBy(vararg types: TypeName) = asTypeName().let { if (types.isEmpty()) it else it.parameterizedBy(*types) }

fun KClass<*>.parameterizedBy(vararg types: Type) = asTypeName().parameterizedBy(types.map { it.asTypeName() })

fun KClass<*>.parameterizedByOut(vararg types: Type) = asTypeName().parameterizedBy(types.map { it.asTypeName(out = true) })

fun Collection<String>.joinToCode(
format: String = "%S",
separator: CharSequence = ", ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ open class KotlinEntitySerializer @Inject constructor(
}
val superType = when (model.originalCategory) {
TypeCategory.BOOLEAN, TypeCategory.STRING -> pathType.asTypeName()
else -> pathType.parameterizedBy(model)
else -> pathType.parameterizedByOut(model)
}
return TypeSpec.classBuilder(mappings.getPathClassName(model, model))
.addAnnotations(model.annotations.map { AnnotationSpec.get(it) })
Expand Down

0 comments on commit 594f72a

Please sign in to comment.