mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 16:09:21 +08:00
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:
@@ -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
|
||||
|
Reference in New Issue
Block a user