jasminb / jsonapi-converter

JSONAPI-Converter is a Java/Android library that provides support for working with JSONAPI spec
Apache License 2.0
272 stars 81 forks source link

Polymorphic relationship fields failing to register with ResourceConverter #219

Closed mea-licensing closed 4 years ago

mea-licensing commented 4 years ago

I'm trying to use polymorphic relationships with Kotlin, but when registering the interface type relationship, it fails with:

java.lang.ClassCastException: libcore.reflect.WildcardTypeImpl cannot be cast to java.lang.Class

This is the same issue when trying to use an open class as a relationship type, as the targetType becomes List<? extends AssetHolder>

Is there a way to use polymorphic relationships with Kotlin?

Relevant code attached

open class BaseJsonModel(
    @Id
    var id: String = ""
)

interface AssetHolder {}

@Type("Image")
class Image : BaseJsonModel(), AssetHolder {
    @JsonProperty("url")
    var url: String? = null
}

@Type("Asset")
class Asset : BaseJsonModel(), AssetHolder {
    @JsonProperty("name")
    var name: String? = null

    @JsonProperty("size")
    var size: Long? = null
}

@Type("Message")
class Message : BaseJsonModel() {
    @Relationship("attachments")
    var attachments: List<AssetHolder>? = null
}
jasminb commented 4 years ago

@mea-licensing open classes as relationship types are not working; this was something that was identified a while ago. Sadly I'm not using Kotlin, which makes it difficult for me to figure out whats going on. If you find a solution, please post it here or make it part of the README.md for others to benefit.