Search before asking
- [x] I searched in the issues and found nothing similar.
Describe the bug
Our unit tests caught regression in Jackson behaviour when upgrading to 2.18.3. We used ConstructorDetector.USE_PROPERTIES_BASED to prevent cases where json string can be used instead of proper json object when class contains only one string field. After migration to 2.18.3 our tests started to fail as ConstructorDetector.USE_PROPERTIES_BASED stopped protecting from this behaviour
Version Information
2.18.3
Reproduction
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class JacksonSerializationTest {
// succeeds with 2.12.7
// also succeeds with 2.17.0, 2.17.2, 2.18.0, 2.18.1, 2.18.2
// fails with 2.18.3+
@Test
public void testSerialization() {
String json = "{\"someFiled\":\"imSomeStringVal\"}";
ObjectMapper objectMapper = JsonMapper.builder()
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
.build();
// no exception starting from 2.18.3
assertThrows(JsonMappingException.class, () -> {
objectMapper.readValue(json, WrapperClass.class);
});
}
public static class WrapperClass {
@JsonProperty("someFiled")
private final SingleStringClass someFiled;
private WrapperClass() {
someFiled = null;
}
public WrapperClass(SingleStringClass someFiled) {
this.someFiled = someFiled;
}
}
public static class SingleStringClass {
@JsonProperty("someStringVal")
private final String someStringVal;
private SingleStringClass() {
someStringVal = null;
}
public SingleStringClass(String someStringVal) {
this.someStringVal = someStringVal;
}
}
}
Expected behavior
No response
Additional context
No response
Comment From: pjfanning
Maybe related to https://github.com/FasterXML/jackson-databind/issues/4860 - a fix made for 2.18.3
Comment From: Saljack
I can also confirm it. It works in 2.17.2. 2.18.0 - 2.18.2 have the issue described in #4860 with multiple constructors. The multiple constructor issue was fixed in 2.18.3, but the fix enabled one string construction for ConstructorDetector.USE_PROPERTIES_BASED
.
Comment From: JooHyukKim
Filed failing test for https://github.com/FasterXML/jackson-databind/pull/5213. Feels like almost as if this issue is conflicting with #4860.