Skip to content
Open
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 @@ -105,12 +105,12 @@ internal fun TypeName.asTypeBlock(): CodeBlock {
return if (rawType == ARRAY) {
val componentType = typeArguments[0]
if (componentType is ParameterizedTypeName) {
// "generic" array just uses the component's raw type
// java.lang.reflect.Array.newInstance(<raw-type>, 0).javaClass
// Render parameterized array components recursively.
// java.lang.reflect.Array.newInstance(<component-type>, 0).javaClass
CodeBlock.of(
"%T.newInstance(%L, 0).javaClass",
Array::class.java.asClassName(),
componentType.rawType.asTypeBlock(),
componentType.asTypeBlock(),
)
} else {
CodeBlock.of("%T::class.java", copy(nullable = false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ class DefaultConstructorTest {
val instance = Moshi.Builder().build().adapter<TestClass>().fromJson(json)
check(instance == expected) { "No match:\nActual : $instance\nExpected: $expected" }
}

@Test
fun nestedArrayWithDefault() {
val instance = Moshi.Builder().build().adapter<NestedArrayTestClass>().fromJson("{}")
check(instance.nestedArray.isEmpty())
}
}

@JsonClass(generateAdapter = true)
data class NestedArrayTestClass(val nestedArray: Array<Array<String>> = emptyArray())

@JsonClass(generateAdapter = true)
data class TestClass(
val required: String,
Expand Down