Skip to content

@JsonSkip is not appliable to Kotlin interface property #222

@mitasov-ra

Description

@mitasov-ra

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang: kotlinRelated for Kotlin languagemodule: jsonRelated to module - json

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions