(note: replacement for #5246)
Properties-based Constructor auto-detection works reliably for 2.x (iff jackson-parameter-names
Module registered) and 3.x (without anything additional), for cases like this class:
public class Point {
private final int x, y;
public Point(int x, int y) {
this.x = x;
this.y. = y;
}
}
(where 2-parameter Constructor is auto-detected for use without need for @JsonCreator
or @JsonProperty
annotations)
this fails if there are any other Constructors. And in particular, this will NOT auto-detect 2-parameter Constructor:
public class Point {
private final int x, y;
public Point() { this(0, 0);
// ^^^ prevents auto-detection; will be used instead!
public Point(int x, int y) {
this.x = x;
this.y. = y;
}
}
But we can change rules to allow this specific case so that auto-detection will work if:
- There is ONE visible (by default visibility rules,
public
) parameters-taking Constructor - That Constructor has names for all Parameters (implicit from bytecode, or annotated)
- There MAY be a no-parameter (any visibility) -- aka "default" -- Constructor
where (3) is the changed rule (CANNOT -> MAY)
NOTE: for backwards-compatibility, there needs to be a way disable this feature:
ObjectMapper mapper = JsonMapper.nbuilder()
.constructorDetector(ConstructorDetector.DEFAULT
// by default is enabled, but we can disable
.withAllowImplicitWithDefaultConstructor(false))
.build();
to revert to 2.x handling where auto-detection is disabled.
Comment From: cowtowncoder
Fixed via #5308.