Skip to content

Jackson JsonIgnore does not work in KotlinΒ #308

@VSMent

Description

@VSMent

Trying to hide DTO id field and assign JSON id field to DTO cityId

build.gradle (there are some spring related which are omitted here)

//plugins 
    id 'org.jetbrains.kotlin.jvm' version '1.3.70'
    id "org.jetbrains.kotlin.plugin.jpa" version "1.3.70"
    id "org.jetbrains.kotlin.plugin.noarg" version "1.3.70"
    id "org.jetbrains.kotlin.plugin.spring" version "1.3.70"
    id "org.jetbrains.kotlin.plugin.allopen" version "1.3.70"

// dependencies
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation "org.jetbrains.kotlin:kotlin-reflect"
    implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.10.2"

TestDto.kt

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty

@JsonIgnoreProperties(ignoreUnknown = true)
data class TestDto(
        @JsonIgnore
        var id: Long? = null,
        var cityId: Int? = null
) {
    @JsonProperty("id")
    private fun unpackId(idObj: Int?) {
        cityId = idObj
    }
}

Test case that fails

    @Test
    fun createTestDto() {
        val dto:TestDto = jacksonObjectMapper().readValue("""{"id":12345}""")
        println(dto)
        assertThat(dto)
                .isNotNull
                .extracting("id").isEqualTo(null)
                .extracting("cityId").isEqualTo(12345)
    }

DTO Object is TestDto(id=12345, cityId=null) instead of TestDto(id=null, cityId=12345)

There was similar issue in FasterXML/jackson-module-kotlin/#124 that is present here #2001, but it should have been resolved till v2.10.2 by #2002

I have opened Stack Overflow question with actual code I am working on, but It seems to be module related issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions