Fix support for classes w/ multiple constructors in code gen (#976)

* Fix broken test

This test suite doesn't run on CI builds but fails locally since the method was moved

* Add multiple constructors test case

* Implement TypeName.asTypeBlock()

* Make DEFAULT_CONSTRUCTOR_MARKER public

* Look up constructor via getDeclaredConstructor with exact param types

Resolves #975

* Remove dead code
This commit is contained in:
Zac Sweers
2019-10-30 00:39:08 -04:00
committed by GitHub
parent 6acebfaca6
commit 4f1c8a5eda
5 changed files with 82 additions and 10 deletions

View File

@@ -322,6 +322,26 @@ class DualKotlinTest(useReflection: Boolean) {
@JsonClass(generateAdapter = true)
class InternalAbstractProperty(override val test: String) : InternalAbstractPropertyBase()
// Regression test for https://github.com/square/moshi/issues/975
@Test fun multipleConstructors() {
val adapter = moshi.adapter<MultipleConstructorsB>()
assertThat(adapter.toJson(MultipleConstructorsB(6))).isEqualTo("""{"f":{"f":6},"b":6}""")
@Language("JSON")
val testJson = """{"b":6}"""
val result = adapter.fromJson(testJson)!!
assertThat(result.b).isEqualTo(6)
}
@JsonClass(generateAdapter = true)
class MultipleConstructorsA(val f: Int)
@JsonClass(generateAdapter = true)
class MultipleConstructorsB(val f: MultipleConstructorsA = MultipleConstructorsA(5), val b: Int) {
constructor(f: Int, b: Int = 6): this(MultipleConstructorsA(f), b)
}
}
// Has to be outside since inline classes are only allowed on top level