mirror of
https://github.com/fankes/moshi.git
synced 2025-10-18 23:49:21 +08:00
Update to KotlinPoet 1.4.2 (#972)
* Unwrap typealiases This was a change in 1.4.2 left over from moshi's prior metadata logic. We need the unwrapped type for class references * Add toe-hold for testing shared adapter behavior * Add regression test for abstractSuperProperties * Kotlinpoet 1.4.2
This commit is contained in:
@@ -18,6 +18,7 @@ package com.squareup.moshi.kotlin.codegen
|
||||
import com.squareup.kotlinpoet.AnnotationSpec
|
||||
import com.squareup.kotlinpoet.ClassName
|
||||
import com.squareup.kotlinpoet.KModifier
|
||||
import com.squareup.kotlinpoet.TypeName
|
||||
import com.squareup.kotlinpoet.TypeSpec
|
||||
import com.squareup.kotlinpoet.asClassName
|
||||
import com.squareup.kotlinpoet.asTypeName
|
||||
@@ -30,8 +31,10 @@ import com.squareup.kotlinpoet.metadata.isInner
|
||||
import com.squareup.kotlinpoet.metadata.isLocal
|
||||
import com.squareup.kotlinpoet.metadata.isSealed
|
||||
import com.squareup.kotlinpoet.metadata.specs.ClassInspector
|
||||
import com.squareup.kotlinpoet.metadata.specs.TypeNameAliasTag
|
||||
import com.squareup.kotlinpoet.metadata.specs.toTypeSpec
|
||||
import com.squareup.kotlinpoet.metadata.toImmutableKmClass
|
||||
import com.squareup.kotlinpoet.tag
|
||||
import com.squareup.moshi.Json
|
||||
import com.squareup.moshi.JsonQualifier
|
||||
import com.squareup.moshi.kotlin.codegen.api.DelegateKey
|
||||
@@ -207,7 +210,8 @@ private fun declaredProperties(
|
||||
): Map<String, TargetProperty> {
|
||||
|
||||
val result = mutableMapOf<String, TargetProperty>()
|
||||
for (property in kotlinApi.propertySpecs) {
|
||||
for (initialProperty in kotlinApi.propertySpecs) {
|
||||
val property = initialProperty.toBuilder(type = initialProperty.type.unwrapTypeAlias()).build()
|
||||
val name = property.name
|
||||
val parameter = constructor.parameters[name]
|
||||
result[name] = TargetProperty(
|
||||
@@ -308,3 +312,7 @@ private fun List<AnnotationSpec>?.jsonName(): String? {
|
||||
private fun String.escapeDollarSigns(): String {
|
||||
return replace("\$", "\${\'\$\'}")
|
||||
}
|
||||
|
||||
private fun TypeName.unwrapTypeAlias(): TypeName {
|
||||
return tag<TypeNameAliasTag>()?.type ?: this
|
||||
}
|
||||
|
@@ -15,10 +15,10 @@
|
||||
*/
|
||||
package com.squareup.moshi.kotlin.codegen
|
||||
|
||||
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
|
||||
import com.tschuchort.compiletesting.KotlinCompilation
|
||||
import com.tschuchort.compiletesting.SourceFile
|
||||
import com.tschuchort.compiletesting.SourceFile.Companion.kotlin
|
||||
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.Ignore
|
||||
import org.junit.Rule
|
||||
@@ -350,6 +350,30 @@ class JsonClassCodegenProcessorTest {
|
||||
assertThat(result.messages).contains("JsonQualifier @UpperCase must have RUNTIME retention")
|
||||
}
|
||||
|
||||
@Ignore("Toe-hold test for when " +
|
||||
"https://github.com/tschuchortdev/kotlin-compile-testing/issues/28 is resolved.")
|
||||
@Test
|
||||
fun `TypeAliases with the same backing type should share the same adapter`() {
|
||||
val result = compile(kotlin("source.kt",
|
||||
"""
|
||||
import com.squareup.moshi.JsonClass
|
||||
|
||||
typealias FirstName = String
|
||||
typealias LastName = String
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
data class Person(val firstName: FirstName, val lastName: LastName, val hairColor: String)
|
||||
"""
|
||||
))
|
||||
assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
|
||||
val adapterSource = result.generatedFiles.find { it.name == "PersonJsonAdapter.kt" }!!
|
||||
|
||||
//language=kotlin
|
||||
assertThat(adapterSource.readText()).isEqualTo("""
|
||||
// TODO implement this
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
private fun prepareCompilation(vararg sourceFiles: SourceFile): KotlinCompilation {
|
||||
return KotlinCompilation()
|
||||
.apply {
|
||||
|
@@ -302,6 +302,26 @@ class DualKotlinTest(useReflection: Boolean) {
|
||||
class TextAsset : Asset<TextAsset>()
|
||||
abstract class Asset<A : Asset<A>>
|
||||
abstract class AssetMetaData<A : Asset<A>>
|
||||
|
||||
// Regression test for https://github.com/square/moshi/issues/968
|
||||
@Test fun abstractSuperProperties() {
|
||||
val adapter = moshi.adapter<InternalAbstractProperty>()
|
||||
|
||||
@Language("JSON")
|
||||
val testJson = """{"test":"text"}"""
|
||||
|
||||
assertThat(adapter.toJson(InternalAbstractProperty("text"))).isEqualTo(testJson)
|
||||
|
||||
val result = adapter.fromJson(testJson)!!
|
||||
assertThat(result.test).isEqualTo("text")
|
||||
}
|
||||
|
||||
abstract class InternalAbstractPropertyBase {
|
||||
internal abstract val test: String
|
||||
}
|
||||
|
||||
@JsonClass(generateAdapter = true)
|
||||
class InternalAbstractProperty(override val test: String) : InternalAbstractPropertyBase()
|
||||
}
|
||||
|
||||
// Has to be outside since inline classes are only allowed on top level
|
||||
|
2
pom.xml
2
pom.xml
@@ -37,7 +37,7 @@
|
||||
<okio.version>1.16.0</okio.version>
|
||||
<okio2.version>2.1.0</okio2.version>
|
||||
<kotlin.version>1.3.50</kotlin.version>
|
||||
<kotlinpoet.version>1.4.1</kotlinpoet.version>
|
||||
<kotlinpoet.version>1.4.2</kotlinpoet.version>
|
||||
<kotlinx-metadata.version>0.1.0</kotlinx-metadata.version>
|
||||
<maven-assembly.version>3.1.0</maven-assembly.version>
|
||||
|
||||
|
Reference in New Issue
Block a user