Skip to content

ParameterNamesModule causes @ConstructorProperties to be ignored, constructor called with null value #4310

@lbenedetto

Description

@lbenedetto

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

If ParameterNamesModule is enabled, the constructor is called with a null value which causes deserialization to fail if the constructor has a null check. However, if the null value is ignored the resulting deserialized object has a non-null value.

Version Information

2.15.2, 2.16.1

Reproduction

Given a class:

import java.beans.ConstructorProperties;

public final class TestClass {

  @ConstructorProperties("value")
  public TestClass(String somethingElse) {
    if (somethingElse == null) {
      System.out.println("Constructor called with null value");
    }
    this.value = somethingElse;
  }

  private final String value;

  public String getValue() {
    return this.value;
  }
}

And a unit test:

import static org.assertj.core.api.BDDAssertions.then;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

class TestCase {
  @Test
  void test() throws Exception {
    // Given
    var object = new TestClass("test");
    var mapper = new ObjectMapper();
    mapper.registerModule(new ParameterNamesModule());

    // When
    var toString = mapper.writeValueAsString(object);
    var fromString = mapper.readValue(toString, TestClass.class);

    // Then
    then(fromString.getValue()).isEqualTo("test");
  }
}

The test passes, but it prints

Constructor called with null value

If my class had a null check in the constructor, then an exception would be thrown even though the end result of the deserialization would have been correct and resulted in a non-null value.

This issue disappears if I remove the ParameterNamesModule, rename the constructor parameter to match the field name, or add @JsonProperty("value").

Expected behavior

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18Issues planned at 2.18 or later

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions