Skip to content

Single argument constructor with single parameter does not work ? #8

@itsmoonrack

Description

@itsmoonrack

Hi,

I have a case where I want to create an immutable object through constructor. The problem I found is that it get passed a null value first, then after try to set the field with the correct one, is this intended behavior ?

Here is a simple test to help you understand the problem:

public class JacksonTest {

    @Test
    public void TestReadValue() throws IOException {
        ObjectMapper objectMapper = new ObjectMapper()
                .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
                .setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.PUBLIC_ONLY)
                .registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));

        ImmutableIdentity identity = objectMapper.readValue("{\"id\":\"ABCDEF\"}", ImmutableIdentity.class);

        assertEquals("ABCDEF", identity.id);
    }

    private static final class ImmutableIdentity {

        private final String id;

        public ImmutableIdentity(final String id) {
            Objects.requireNonNull(id, "The id must not be null.");

            this.id = id;
        }
    }

}

Here is the stack

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of JacksonTest$ImmutableIdentity, problem: The id must not be null.
at [Source: {"id":"ABCDEF"}; line: 1, column: 15]

If I comment the Objects.requiredNonNull, the test passes. But I need that in my constructor since its part of my serialized Domain Events.

Any help is welcome. Also if its not supported I can work on a patch if you give me some guidelines.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions