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

Java records don't serialize with MapperFeature.REQUIRE_SETTERS_FOR_GETTERS since 2.16 #4678

Closed
1 task done
mathijs81 opened this issue Aug 22, 2024 · 3 comments
Closed
1 task done
Labels
Milestone

Comments

@mathijs81
Copy link

mathijs81 commented Aug 22, 2024

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When a mapper is configured with REQUIRE_SETTERS_FOR_GETTERS, nothing gets serialized for java records, even though the properties are just properties, not just getters.

Version Information

2.16.0

Reproduction

Here's a test that reproduces the issue. This test passes on 2.15.3, but starts failing from 2.16.0 onwards.
(The failure is that {} is returned instead of {"prop":123}.)

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

class SerializeTest {
    record TestRecord(Object prop) {}

    @Test
    void testSerialize() throws Exception {
        var mapper = JsonMapper.builder().build();
        var mapperWithSetters = JsonMapper.builder().configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true).build();

        assertEquals("{\"prop\":123}", mapper.writeValueAsString(new TestRecord(123)));
        // This one fails for 2.16.0, works for 2.15.3:
        assertEquals("{\"prop\":123}", mapperWithSetters.writeValueAsString(new TestRecord(123)));
    }

}

Expected behavior

REQUIRE_SETTERS_FOR_GETTERS shouldn't matter for java records

Additional context

I'm running JDK 17 if it matters.

@mathijs81 mathijs81 added the to-evaluate Issue that has been received but not yet evaluated label Aug 22, 2024
@cowtowncoder
Copy link
Member

Just to make sure I understand: this also fails in 2.17(.2)?

Other than that, yeah... seems like Records should ignore this setting.

@cowtowncoder cowtowncoder added 2.17 Issues planned at earliest for 2.17 and removed to-evaluate Issue that has been received but not yet evaluated labels Aug 23, 2024
@mathijs81
Copy link
Author

mathijs81 commented Aug 23, 2024

Yes, also fails in 2.17.2 (and also with latest LTS JDK 21)

@cowtowncoder cowtowncoder added 2.18 and removed 2.17 Issues planned at earliest for 2.17 labels Aug 24, 2024
@cowtowncoder cowtowncoder added this to the 2.18.0 milestone Aug 24, 2024
@cowtowncoder
Copy link
Member

Looks like this indeed failed on 2.17.x, but was fixed by something (POJO property introspection rewrite?) in 2.18 branch. Will add passing test, mark as fixed.

@cowtowncoder cowtowncoder changed the title java records don't serialize with REQUIRE_SETTERS_FOR_GETTERS since 2.16.0 Java records don't serialize with MapperFeature.REQUIRE_SETTERS_FOR_GETTERS since 2.16 Aug 24, 2024
cowtowncoder added a commit that referenced this issue Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants