Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson JsonIgnore does not work in Kotlin #308

Closed
VSMent opened this issue Mar 13, 2020 · 4 comments
Closed

Jackson JsonIgnore does not work in Kotlin #308

VSMent opened this issue Mar 13, 2020 · 4 comments
Milestone

Comments

@VSMent
Copy link

VSMent commented Mar 13, 2020

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.

@cowtowncoder
Copy link
Member

Please file these issues under jackson-module-kotlin in future, unless they can be reproduced with just databind. I will transfer this now.

@cowtowncoder cowtowncoder transferred this issue from FasterXML/jackson-databind Mar 13, 2020
@VSMent
Copy link
Author

VSMent commented Mar 14, 2020

@cowtowncoder ok. I was not really sure which module it belongs.

@hiddewie
Copy link
Contributor

I added the test case as given in this issue locally, but it passes and the output seems correct.

See #387

dinomite added a commit that referenced this issue Oct 18, 2020
(#308) Add test for JsonProperty hiding
@dinomite dinomite added the 2.11 label Oct 18, 2020
@dinomite
Copy link
Member

Fixed by #388 in 2.12

@dinomite dinomite added this to the 2.12.0 milestone Oct 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants