Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix self-referencing typevars #151

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}