Skip to content

Deserialization behavior change with Records, @JsonCreator and @JsonValue between 2.17 and 2.18 #4724

@arlampin

Description

@arlampin

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

Using both JsonCreator and JsonValue annotations with Java Records makes ObjectMapper seemingly skip single-arg static JsonCreator method and try to contract instance directly. I couldn't find any hint that this was a planned change.

Version Information

2.18.0

Reproduction

The following code works with 2.17.2, but fails with 2.18.0

    @Test
    void deserialization() throws Exception {
        new ObjectMapper().readValue("\"\"", Something.class);
    }

    public record Something(String value) {

        public Something {
            if (value == null || value.isEmpty()) {
                throw new IllegalArgumentException("Value cannot be null or empty");
            }
        }

        @JsonCreator
        public static Something of(String value) {
            if (value.isEmpty()) {
                return null;
            }
            return new Something(value);
        }

        @Override
        @JsonValue
        public String toString() {
            return value;
        }

    }

Removing JsonValue annotation makes deserialization to work again.

Expected behavior

No response

Additional context

No response

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