square/moshi

Field serialized despite of the Transient annotation

alizeec opened this issue · 0 comments

Hello!
I have an issue with the @Transient annotation. I'm trying to serialize this model. The field "type" is in the payload, where it shouldn't be because of the @Transient annotation, correct?

@JsonClass(generateAdapter = true)
data class ApiNewReviewCollectionBodyRequest(
    @Json(name = "identifier")
    val id: String,
    @Json(name = "value")
    val value: ApiBodyRequestValue,
)

interface ApiBodyRequestValue {
    val type: String

    companion object {
        val adapterFactory: PolymorphicJsonAdapterFactory<ApiBodyRequestValue> =
            PolymorphicJsonAdapterFactory.of(ApiBodyRequestValue::class.java, "type")
                .withSubtype(
                    ValueRate::class.java,
                    "rate",
                )
                .withSubtype(
                    ValueComment::class.java,
                    "comment",
                )
    }
}

@JsonClass(generateAdapter = true)
data class ValueRate(val rate: Float) :
    ApiBodyRequestValue {
    @Transient
    override val type = "rate"
}

@JsonClass(generateAdapter = true)
data class ValueComment(val comment: String) :
    ApiBodyRequestValue {
    @Transient
    override val type =  "comment"
}

gives me

id: "ID",
value: {
   rate: 4
   type: "rate"
}

and I want

id: "ID",
value: {
   rate: 4
}

Do you have any idea why ?

Thanks