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

@JsonProperty not honored in superclass fields #474

Closed
marshallpierce opened this issue Jul 5, 2021 · 7 comments
Closed

@JsonProperty not honored in superclass fields #474

marshallpierce opened this issue Jul 5, 2021 · 7 comments
Labels
2.12 Issue affecting version 2.12 2.13 Issue affecting version 2.13 bug
Milestone

Comments

@marshallpierce
Copy link

Describe the bug
Controlling the serialized field name with @JsonProperty doesn't appear to be working for fields in a superclass.

To Reproduce

open class Parent(@JsonProperty("parent-prop") val parent: String)
class Child(@JsonProperty("child-prop") val child: String) : Parent(child)

println(jacksonObjectMapper().writeValueAsString(Child("foo")))

yields {"child-prop":"foo","parent":"foo"}.

Expected behavior

I would expect to see {"child-prop":"foo","parent-prop":"foo"}.

Versions
Kotlin: 1.5.20
Jackson-module-kotlin: 2.12.3
Jackson-databind: 2.12.3

@dinomite dinomite added the 2.12 Issue affecting version 2.12 label Jul 5, 2021
@dinomite
Copy link
Member

dinomite commented Jul 5, 2021

That’s pretty interesting. Seems likely a recent issue or we would have seen this previously.

@dinomite dinomite added this to the 2.13.0 milestone Jul 5, 2021
@marshallpierce
Copy link
Author

marshallpierce commented Jul 5, 2021

wild guess: changes in kotlin reflection behavior in 1.5?

edit: or maybe it's in jackson-databind and also affects Java? 🤷

dinomite added a commit that referenced this issue Jul 6, 2021
@dinomite
Copy link
Member

dinomite commented Jul 6, 2021

Happens in j-m-k 2.12 which is based on Kotlin 1.4.21 🤔

@dinomite dinomite added the 2.13 Issue affecting version 2.13 label Jul 6, 2021
@dinomite
Copy link
Member

dinomite commented Jul 6, 2021

Hmm, also happens in 2.11 (Kotlin 1.3.72)

@dinomite
Copy link
Member

dinomite commented Jul 6, 2021

Not a databind issue, this works in Databind on 2.12:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class JacksonModuleKotlinGithub474 {
    static class Parent {
        @JsonProperty("parent-prop")
        String parent;

        public Parent(String parent) {
            this.parent = parent;
        }
    }

    static class Child extends Parent {
        @JsonProperty("child-prop")
        String child;

        public Child(String child) {
            super(child);
            this.child = child;
        }
    }

    @Test
    public void jsonPropertyAnnotationRespectedOnParentClass() throws Exception {
        assertEquals(
                "{\"parent-prop\":\"foo\",\"child-prop\":\"foo\"}",
                (new ObjectMapper()).writeValueAsString(new Child("foo"))
        );
    }
}

@vsigler
Copy link

vsigler commented Jul 19, 2022

Any update around this issue? Currently seeing it with 2.13.3 and Kotlin 1.6.

I've tried a number of older versions, but I've seen the problem with each. The earliest I tried was 2.10.x.

What works is to annotate the getter method, but that does not seem like the proper approach.

@get:JsonProperty("myPropName")

@k163377
Copy link
Contributor

k163377 commented Apr 2, 2023

It is closed as a duplicate of #658.

@k163377 k163377 closed this as completed Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.12 Issue affecting version 2.12 2.13 Issue affecting version 2.13 bug
Projects
None yet
Development

No branches or pull requests

4 participants