Moshi: Kotlin Codegen - Named companion objects fail the build

Created on 16 May 2018  路  2Comments  路  Source: square/moshi

When I create a @JsonClass(generateAdapter = true) class Foo and add the default Parcelable implementation quick fix the project will not compile. The same will happen when I simply give my companion object a different name.

FooJsonAdapter.kt: (11, 9): Unresolved reference: Companion

The extension function is using the wrong name for the companion object.


Example with a named companion object:

@JsonClass(generateAdapter = true)
class Foo {
    companion object Bar
}

Line with the error in FooJsonAdapter, should be Foo.Bar to compile:

fun Foo.Companion.jsonAdapter(moshi: Moshi): JsonAdapter<Foo> = FooJsonAdapter(moshi)

The extension method should be either removed in this case or it should use the custom name of the companion object.

bug

Most helpful comment

Yikes. Will fix.

All 2 comments

Yikes. Will fix.

Faced the same issue.

Although, @bleeding182 in your case, Using @Parcelize instead of adding the boiler-plate Parcelable implementation can be a way-out for the issue until 1.7 release.

This was my use case:

@Parcelize
@JsonClass(generateAdapter = true)
data class Foo {
    val bar: String,
    // ........ 
}
Was this page helpful?
0 / 5 - 0 ratings