Skip to content

Exception when deserialization of private record with default constructor #4175

Closed
@janpachol

Description

@janpachol

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

When deserializing a record, jackson fails with

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of ... (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

It looks like jackson does not see default constructor.

Version Information

2.16.0-rc1

Reproduction

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

class TestObjectTest {

    @Test
    public void shouldDeserialize() throws JsonProcessingException {
        TestObject testObject = new ObjectMapper().readValue("{\"text\":\"anything\"}", TestObject.class);
        Assertions.assertEquals("anything", testObject.text());
    }

    private record TestObject(String text) {}
}

When I add compact constructor with @JsonCreator to TestObject, it works correctly:

    private record TestObject(String text) {
        @JsonCreator
        private TestObject {
        }
    }

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    RecordIssue 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