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

Cached Optional serializer does not apply annotations for POJO properties #17

Closed
codicusmaximus opened this issue Mar 10, 2017 · 4 comments
Milestone

Comments

@codicusmaximus
Copy link

codicusmaximus commented Mar 10, 2017

Simple test case:

public class JsonObjectTest {
    
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
    private Optional<Date> date1;
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM")
    private Optional<Date> date2;
    
    public JsonObjectTest () {}

    public Optional<Date> getDate1() {
        return date1;
    }
    public void setDate1(Optional<Date> date1) {
        this.date1 = date1;
    }
    public Optional<Date> getDate2() {
        return date2;
    }
    public void setDate2(Optional<Date> date2) {
        this.date2 = date2;
    }
    
}
public class Main {
    public static void main(String[] args) throws JsonProcessingException {
        JsonObjectTest t = new JsonObjectTest();
        t.setDate1(Optional.ofNullable(new Date()));
        t.setDate2(Optional.ofNullable(new Date()));
        ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module());
        System.out.println(mapper.writeValueAsString(t));
    }
}

produces the output:
{"date1":"2017-03-10","date2":"2017-03-10"}

changing the order of the members to:

    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM")
    private Optional<Date> date2;
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd")
    private Optional<Date> date1;

produces the output:
{"date2":"2017-03","date1":"2017-03"}

I have found that the issue lies in OptionalSerializer.java on the ser = _findCachedSerializer(provider, value.getClass()); line. The first time a Date is encountered, a new serializer is constructed which observes the relevant annotation; however, every time a Date is encountered afterwards the cached serializer is used without any consideration regarding annotation differences.

@codicusmaximus
Copy link
Author

codicusmaximus commented Mar 10, 2017

I believe all that's needed is
ser = (JsonSerializer<Object>)provider.handlePrimaryContextualization(ser, _property);
after the _findCachedSerializer line. I will test this and submit a pull request.

@cowtowncoder
Copy link
Member

cowtowncoder commented Mar 10, 2017

Appears to affect 2.8 at least. Due to repo refactoring needs to be applied in 2 places, which is fine, I can handle the other one.
Added some notes on suggested patch, but when I get a chance I can have better look at changes.

cowtowncoder added a commit that referenced this issue Mar 13, 2017
@cowtowncoder cowtowncoder added this to the 2.8.8 milestone Mar 14, 2017
@cowtowncoder
Copy link
Member

Ok, found a simpler fix which I think is proper; different call to SerializerProvider that handles contextualization call as expected.

@cowtowncoder cowtowncoder changed the title Cached serializer doesn't take into account differing annotations for POJO members / bean properties Cached Optional serializer does not apply annotations for POJO properties Mar 14, 2017
@codicusmaximus
Copy link
Author

Sounds good, thank you very much!

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

No branches or pull requests

2 participants