-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constructor autodetection does not take naming strategy into account #3846
Comments
Seems like problem is with detecting properties and registering static class PointFail {
private int xNum;
private int yNum;
public PointFail(int xNum, int yNum) {
this.xNum = xNum;
this.yNum = yNum;
}
}
static class PointSuccess {
private int xNum;
private int yNum;
public PointSuccess(int xNum, int yNum) {
this.xNum = xNum;
this.yNum = yNum;
}
public void setxNum(int xNum) {
this.xNum = xNum;
}
public void setyNum(int yNum) {
this.yNum = yNum;
}
}
// with `jackson-module-parameter-names` module
ObjectMapper mapper = JsonMapper.builder()
.addModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES))
.constructorDetector(ConstructorDetector.USE_PROPERTIES_BASED)
.propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
.build();
// Fails
public void testPointFails() throws Exception {
PointFail strategyBean = mapper
.readValue(a2q("{'x_num':1, 'y_num':2}"), PointFail.class);
assertEquals(strategyBean.xNum, 1);
assertEquals(strategyBean.yNum, 2);
}
// Success, with setters OR getters
public void testPointSuccess() throws Exception {
PointSuccess strategyBean = mapper
.readValue(a2q("{'x_num':1, 'y_num':2}"), PointSuccess.class);
assertEquals(strategyBean.xNum, 1);
assertEquals(strategyBean.yNum, 2);
} |
Yes, @JooHyukKim is right; this is due to issue discussed under #3719 and just one symptom. |
Just a side note - about:
...
|
Asking out of curiousity: if the field is |
The point is that it doesn't have a setter. Getter is irrelevant here. |
It works if there's a getter, though. Why is a setter relevant? |
I mean as pointed out here, naming strategy is used when a getter exists e.g.:
|
If there is a setter Jackson may use that instead of the constructor. |
For naming strategy to work for a "property", one of these is currently needed:
So one of the things you can do to make naming strategy to work with:
...is to add a getter:
...which is pretty normal because how else can that private field be used? I'm just curious about what are you doing with your JSON model class in your project/codebase, for it to have a private field without getter, resulting in you facing this issue? |
This example is about #5.
Except @JsonCreator should not be needed contrary to what you wrote. You can read more about it here https://cowtowncoder.medium.com/jackson-2-12-most-wanted-3-5-246624e2d3d0.
I am not. It was not relevant for the example but I can see it being valid. Not all instance fields need/should have getters 🙃. |
I specifically said "one of these is currently needed"... Meaning, even if you don't have number 5, as long as you have either 1, 2, 3, 4, or 6, naming strategy will work.
OK so you're not reporting for a practical problem (i.e. "I'm facing this issue now!"), but rather an edge-case you found while testing Jackson. I just wanted to know if I'm missing anything I should be careful of as a Jackson user, thanks! |
As per my earlier notes, I think it is very likely this is related to the problem of "implicit" Creators being discovered too late to be properly linked to accessors (getters/setters): explicit (annotation indicated) Creators are discovered first, their arguments get matched properly with getters/setters; implicit ones are discovered at a later phase. This is something that I think requires a rewrite of property discovery (see #3719), and if and when this happens can be resolved. But I do not think smaller incremental fixes are possible. |
Work on #4515 may make this fixable for 2.18. What would be great would be a reproduction (unit test), to help test eventual fix. |
Now that #4515 is fixed, I think this issue is likely covered as well. |
Constructor autodetection (https://cowtowncoder.medium.com/jackson-2-12-most-wanted-3-5-246624e2d3d0/) does not appear to be working when used together with naming strategies which don't match the field names. See the examples below for more details.
The text was updated successfully, but these errors were encountered: