Skip to content

@JsonSkip is not appliable to Kotlin interface property #222

Open
mitasov-ra opened this issue Jul 27, 2023 · 0 comments
Open

@JsonSkip is not appliable to Kotlin interface property #222

mitasov-ra opened this issue Jul 27, 2023 · 0 comments
Labels
lang: kotlin Related for Kotlin language module: json Related to module - json

Comments

@mitasov-ra
Copy link
Contributor

mitasov-ra commented Jul 27, 2023

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.

@GoodforGod GoodforGod added lang: kotlin Related for Kotlin language module: json Related to module - json labels Aug 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang: kotlin Related for Kotlin language module: json Related to module - json
Projects
None yet
Development

No branches or pull requests

2 participants