Skip to content

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

@mathijs81

Description

@mathijs81

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18Issues planned at 2.18 or later

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions