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

@JsonView doesn't work with @JsonCreator #1172

Closed
dbolotin opened this issue Mar 28, 2016 · 4 comments
Closed

@JsonView doesn't work with @JsonCreator #1172

dbolotin opened this issue Mar 28, 2016 · 4 comments
Labels
2.16 Issues planned for 2.16 property-discovery Problem with property discovery (introspection)
Milestone

Comments

@dbolotin
Copy link

Consider the following example:

    public static class View {
    }

    public static class View1 extends View {
    }

    public static class View2 extends View {
    }

    @Test
    public void jacksonTest1() throws Exception {
        ObjectMapper om = new ObjectMapper();
        ObjectWriter w1 = om.writerWithView(View1.class);
        ObjectWriter w2 = om.writerWithView(View2.class);
        ObjectReader r1 = om.readerFor(Obj.class).withView(View1.class);
        ObjectReader r2 = om.readerFor(Obj.class).withView(View2.class);
        Obj o = new Obj("a", "b", "c");
        String so0 = om.writeValueAsString(o);
        System.out.println(so0); // {"a":"a","b":"b","c":"c"}
        String so1 = w1.writeValueAsString(o);
        System.out.println(so1); // {"a":"a","b":"b"}
        String so2 = w2.writeValueAsString(o);
        System.out.println(so2); // {"a":"a","c":"c"}

        System.out.println(r1.readValue(so0).toString()); // See below
    }

(1) Obj definition without @JsonCreator

    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
            isGetterVisibility = JsonAutoDetect.Visibility.NONE,
            getterVisibility = JsonAutoDetect.Visibility.NONE)
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Obj {
        public String a;

        @JsonView(View1.class)
        public String b;

        @JsonView(View2.class)
        public String c;

        public Obj() {
        }

        public Obj() {
        }

        public Obj(String a,
                   String b,
                   String c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

        @Override
        public String toString() {
            return "Obj{" +
                    "a='" + a + '\'' +
                    ", b='" + b + '\'' +
                    ", c='" + c + '\'' +
                    '}';
        }
    }

(2) Obj definition with @JsonCreator

    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
            isGetterVisibility = JsonAutoDetect.Visibility.NONE,
            getterVisibility = JsonAutoDetect.Visibility.NONE)
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Obj {
        public String a;

        @JsonView(View1.class)
        public String b;

        @JsonView(View2.class)
        public String c;

        public Obj() {
        }

        @JsonCreator
        public Obj(@JsonProperty("a") String a,
                   @JsonProperty("b") String b,
                   @JsonProperty("c") String c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }

        @Override
        public String toString() {
            return "Obj{" +
                    "a='" + a + '\'' +
                    ", b='" + b + '\'' +
                    ", c='" + c + '\'' +
                    '}';
        }
    }

The problem:

Example works with first (1) variant of Obj definition. And outputs:

Obj{a='a', b='b', c='null'}

but fails to ignore field c with Obj defined with JsonCreator (2):

Obj{a='a', b='b', c='c'}
@Praytic
Copy link

Praytic commented Jul 11, 2022

If someone also experiencing the same issue here's the temporary fix. You can put the @JsonView annotation on every method's parameter.

@JsonCreator
public Obj(@JsonProperty("a") String a,
           @JsonProperty("b") @JsonView(View1.class) String b,
           @JsonProperty("c") @JsonView(View2.class) String c) {
    this.a = a;
    this.b = b;
    this.c = c;
}

@cowtowncoder cowtowncoder added the property-discovery Problem with property discovery (introspection) label Jul 11, 2022
@cowtowncoder
Copy link
Member

Note: suspecting this is due to issues with combining annotations between "regular" and @JsonCreator (constructor parameter) properties. So it would (need to be) fixed if and when Property Introspection handling is rewritten to allow proper merging of all annotations.

@JooHyukKim
Copy link
Member

#4173 proves this has been fixed, after idk, like 4 versions 🤔

@cowtowncoder cowtowncoder added 2.16 Issues planned for 2.16 and removed 2.15 labels Oct 24, 2023
@cowtowncoder cowtowncoder added this to the 2.16.0 milestone Oct 24, 2023
@cowtowncoder
Copy link
Member

@JooHyukKim better late verification than never :)

@cowtowncoder cowtowncoder modified the milestones: 2.16.0, 2.15.4 Oct 24, 2023
@cowtowncoder cowtowncoder changed the title JsonView doesn't work with JsonCreator @JsonView doesn't work with @JsonCreator Oct 24, 2023
cowtowncoder added a commit that referenced this issue Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.16 Issues planned for 2.16 property-discovery Problem with property discovery (introspection)
Projects
None yet
Development

No branches or pull requests

4 participants