Skip to content

Commit

Permalink
Fix self-referencing typevars
Browse files Browse the repository at this point in the history
Resolves #125
  • Loading branch information
ZacSweers committed Aug 27, 2021
1 parent 8cde3df commit 8891e77
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ internal fun List<KSTypeParameter>.toTypeParameterResolver(
// replacement later that may add bounds referencing this.
val id = typeVar.name.getShortName()
parametersMap[id] = TypeVariableName(id)
}

for (typeVar in this) {
val id = typeVar.name.getShortName()
// Now replace it with the full version.
parametersMap[id] = typeVar.toTypeVariableName(resolver)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ internal fun List<TypeName>.toTypeVariableResolver(
override val parametersMap: Map<String, TypeVariableName> = parametersMap

override operator fun get(index: String): TypeVariableName = typeParamResolver(index)

override fun toString(): String {
return parametersMap.toString()
}
}

// Fill the parametersMap. Need to do sequentially and allow for referencing previously defined params
Expand All @@ -209,6 +213,12 @@ internal fun List<TypeName>.toTypeVariableResolver(
// replacement later that may add bounds referencing this.
val id = typeVar.name
parametersMap[id] = TypeVariableName(id)
}

// Fill the parametersMap. Need to do sequentially and allow for referencing previously defined params
for (typeVar in this) {
check(typeVar is TypeVariableName)
val id = typeVar.name
// Now replace it with the full version.
parametersMap[id] = typeVar.deepCopy(null) { it.stripTypeVarVariance(resolver) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,48 @@ class DualKotlinTest(useReflection: Boolean) {
abstract class Asset<A : Asset<A>>
abstract class AssetMetaData<A : Asset<A>>

// Regression test for https://github.com/ZacSweers/MoshiX/issues/125
@Test fun selfReferencingTypeVars() {
val adapter = moshi.adapter<StringNodeNumberNode>()

val data = StringNodeNumberNode().also {
it.t = StringNodeNumberNode().also {
it.text = "child 1"
}
it.text = "root"
it.r = NumberStringNode().also {
it.number = 0
it.t = NumberStringNode().also {
it.number = 1
}
it.r = StringNodeNumberNode().also {
it.text = "grand child 1"
}
}
}
assertThat(adapter.toJson(data))
//language=JSON
.isEqualTo("""
{"text":"root","t":{"text":"child 1"},"r":{"number":0,"t":{"number":1},"r":{"text":"grand child 1"}}}
""".trimIndent())
}

@JsonClass(generateAdapter = true)
open class Node<T : Node<T, R>, R : Node<R, T>> {
var t : T? = null
var r : R? = null
}

@JsonClass(generateAdapter = true)
class StringNodeNumberNode : Node<StringNodeNumberNode, NumberStringNode>() {
var text: String = ""
}

@JsonClass(generateAdapter = true)
class NumberStringNode : Node<NumberStringNode, StringNodeNumberNode>() {
var number: Int = 0
}

// Regression test for https://github.com/square/moshi/issues/968
@Test fun abstractSuperProperties() {
val adapter = moshi.adapter<InternalAbstractProperty>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ class MoshiKspTest {
@JsonClass(generateAdapter = true)
data class SimpleImpl(override val a : String, val b : String) : Any(), SimpleInterface
}

0 comments on commit 8891e77

Please sign in to comment.