kora icon indicating copy to clipboard operation
kora copied to clipboard

@JsonSkip is not appliable to Kotlin interface property

Open mitasov-ra opened this issue 2 years ago • 0 comments

Assume I have the following type hierarchy:

interface Foo {
  val empty
  
  val notEmpty get() = !empty
}

data class Bar(override val empty) : Foo

If I try to add @ru.tinkoff.kora.json.common.annotation.JsonSkip annotation to notEmpty property, I get the following error:

This annotation is not applicable to target 'member property without backing field or delegate'

However, if I then serialize Bar(false) instance with generated JsonWriter<Bar>, I get the following JSON:

{
  "empty": false,
  "notEmpty": true
}

So, there's currently no way to exclude inherited default property from serialization. The only workaround is to explicitly declare notEmpty inside Bar with backing field like so:

data class Bar(override val empty) : Foo {
  @JsonSkip
  override val optional = !required // notice I'm not using get() anymore, so !required is stored in field
}

But this kills the whole point of creating notEmpty property in interface.

mitasov-ra avatar Jul 27 '23 17:07 mitasov-ra