ansman/kotshi

Wrong error about @GetterName?

autonomousapps opened this issue · 6 comments

I'm seeing this trying to build my project:

MembershipData.java:81: error: Kotshi: Could not find a getter named getIs_premium, annotate the parameter with @GetterName if you use @JvmName
    com.chess.net.model.ExpirationInfo date, int is_premium, int level, @org.jetbrains.annotations.NotNull()
                                                 ^

Here is my code:

@JsonSerializable
class MembershipItem(
    override val data: MembershipData
) : BaseResponseItem<MembershipData>(data = data)

/* SAMPLE JSON
  "date": {
        "expires": 1555570800,
        "last_renewed": 1524034800,
        "start": 1369258234
    },
    "is_premium": 0,
    "level": 0,
    "result": "success",
    "sku": "gold_monthly",
    "type": "basic",
    "user_id": 41,
    "is_trial_eligible": false
    "is_apple_auto_renewable": false
    "is_google_subscription": false
*/

@JsonSerializable
data class MembershipData(
    val date: ExpirationInfo = ExpirationInfo(),
    val is_premium: Int = 0, // it's complaining about this line, seemingly
    val level: Int = 0,
    val result: String = "",
    val sku: String = "",
    val type: String = "",
    val user_id: Int = 0,
    val is_trial_eligible: Boolean = false,
    val is_apple_auto_renewable: Boolean = false,
    val is_google_subscriber: Boolean = false
) 

@JsonSerializable
data class ExpirationInfo(
    val expires: Long = 0,
    val last_renewed: Long = 0,
    val start: Long = 0
)

I will note that a very similar structure works for other data classes, without issue. Any ideas? I'm using v1.0.2

OH, is it related to this?

Another limitation is that custom getter names for the JVM cannot be accessed from the constructor parameter which requires you to annotate the parameter with @getter. This limitation will be removed when the library starts generating Kotlin code.

This is the first time one of my Kotshi-annotated classes has been accessed from Java.

This is definitely a bug, since Kotshi consumes Java code it needs to find the getter generated for each property. It seems that this logic is broken.

For now you can fix this by either using @Getter or naming it isPremium and annotating it with @Json(name = 'is_premium').

I was able to resolve it (temporarily, I hope!) by the following:

@GetterName("isPremium") @get:JvmName("isPremium")
val is_premium: Int = 0

and later, in the class body

fun isPremium(): Int = is_premium

It's pretty ugly. Maybe I'll try using @Json() instead. However, long-term, I prefer to just leave the underscores as a matter of policy.

You probably just need @GetterName("isPremium")

Anyway, it has been fixed and will be released in the next version.

I tried that first and it didn't work... Actually I didn't have the actual getter at that point. But anyway, yeah, fixed soon. Thanks!

Released in 1.0.3