Skip to content

@JsonProperty not serializing field names properly on @JsonCreator in Record #4452

@Incara

Description

@Incara

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Serialization of java records do not appear to be honoring @JsonProperty names in @JsonCreator constructors.

Version Information

2.15.x+

Reproduction

A test below fails for me and I expect it to pass.

import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class RecordTest {

    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public record TestObject(String testFieldName, Integer testOtherField) {

        @JsonCreator
        public TestObject(@JsonProperty ("strField") String testFieldName,
                          @JsonProperty ("intField") Integer testOtherField) {
            this.testFieldName = testFieldName;
            this.testOtherField = testOtherField;
        }
    }

    @Test
    public void test() throws JsonProcessingException {
        final String result = OBJECT_MAPPER.writeValueAsString(new TestObject("test", 1));
        assertTrue(result.contains("strField"));
    }
}

Expected behavior

The field is renamed to what is specified in the @JsonProperty annotation in the record's @JsonCreator

Additional context

We encountered this upgrading from 2.14.x up to 2.16.x, so this functionality was working at that earlier version (perhaps before records were really addressed).

Perhaps we have versions incorrect between jackson modules and libraries, but we haven't found the exact mismatch yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18Issues planned at 2.18 or laterRecordIssue related to JDK17 java.lang.Record supporthas-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions