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

@JsonDeserialize(converter = ...) does not work with Records #3297

Closed
gavlyukovskiy opened this issue Oct 7, 2021 · 4 comments · Fixed by #3724
Closed

@JsonDeserialize(converter = ...) does not work with Records #3297

gavlyukovskiy opened this issue Oct 7, 2021 · 4 comments · Fixed by #3724
Labels
Record Issue related to JDK17 java.lang.Record support
Milestone

Comments

@gavlyukovskiy
Copy link

Describe the bug
@JsonDeserialize(converter = ...) on Record's fields isn't picked up by Jackson during deserialization. I'm using this converter to parse time without zone information into Instant field.

Version information
Jackson: 2.13, 2.13.1-SNAPSHOT
JDK: 17

To Reproduce

public class JacksonTestCase {

    public static void main(String[] args) throws JsonProcessingException {
        var objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());

        objectMapper.readValue("""
                               { "value": "test", "time": "2021-10-07T22:47:15" }
                                """, RecordEntity.class);
    }

    static record RecordEntity(
            String value,
            @JsonDeserialize(converter = InstantWithoutZoneConverter.class)
            Instant time
    ) {
    }

    static class InstantWithoutZoneConverter implements Converter<LocalDateTime, Instant> {
        @Override
        public Instant convert(LocalDateTime value) {
            return value.toInstant(ZoneOffset.UTC);
        }

        @Override
        public JavaType getInputType(TypeFactory typeFactory) {
            return typeFactory.constructType(LocalDateTime.class);
        }

        @Override
        public JavaType getOutputType(TypeFactory typeFactory) {
            return typeFactory.constructType(Instant.class);
        }
    }
}

Additional context
It looks similar to #2974 and workaround to add @JsonProperty("time") works here as well:

    static record RecordEntity(
            String value,
            @JsonProperty("time")
            @JsonDeserialize(converter = InstantWithoutZoneConverter.class)
            Instant time
    ) {
    }
@gavlyukovskiy gavlyukovskiy added the to-evaluate Issue that has been received but not yet evaluated label Oct 7, 2021
@cowtowncoder cowtowncoder added Record Issue related to JDK17 java.lang.Record support 2.14 and removed to-evaluate Issue that has been received but not yet evaluated labels Oct 7, 2021
@cowtowncoder
Copy link
Member

Yes, unfortunately there are issues with Annotation applicability for Record types. This is due to the way introspection is handled. I am hoping to resolve these for Jackson 2.14, but need time to work on rewriting introspection handling almost from scratch to get there.

@gavlyukovskiy
Copy link
Author

I wish you luck and hope rewrite goes well :)

Thank you for your incredible effort in maintaining and evolving this project!

@cowtowncoder
Copy link
Member

Thank you @gavlyukovskiy! I really appreciate your feedback & hope I can be worth the praise! :)

@Mario-Eis
Copy link

Mario-Eis commented Dec 7, 2021

@cowtowncoder There is also an issue with @JsonView not working with records. Is this the same reason or should I create a new issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Record Issue related to JDK17 java.lang.Record support
Projects
None yet
3 participants