-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Let the DataBinder construct nested objects #20806
Comments
Hans Desmet commented The Java ecosystem is evolving towards immutability. |
I've also come across this and am interested in the enhancement. I've put together a sample that demonstrates some additional use-cases that I've highlighted below. The sample has multiple packages each demonstrating a slightly different usecase around support for immutable With GettersThis is when the immutable type has getter methods and is the same as the original issue. The resulting exception is:
No GettersSometimes immutable objects inject a different value into the constructor than what is exposed via the getter methods. Currently if the custom type does not expose a getter method, the application does not throw an class Line {
private final String description;
private final Point point1;
private final Point point2;
public Line(String description, Point point1, Point point2) {
this.description = description;
this.point1 = point1;
this.point2 = point2;
}
// no getters means Spring MVC binds null values to point1 and point2
} Multiple ConstructorsAt times there are more than one constructor. Currently, Spring MVC will produce an error class Point {
private final String description;
private final int x;
private final int y;
public Point(int x, int y) {
this("default description", x, y);
}
public Point(String description, int x, int y) {
this.description = description;
this.x = x;
this.y = y;
}
public String getDescription() {
return description;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
} BuildersWhen using immutable objects, users often want to leverage Builder objects. It would be nice to be able to support the builders as shown below: class Point {
private final int x;
private final int y;
private Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private int x;
private int y;
private Builder() {
}
public Builder x(int x) {
this.x = x;
return this;
}
public Builder y(int y) {
this.y = y;
return this;
}
public Point build() {
return new Point(x, y);
}
}
} It's worth mentioning that there are a number of different styles of builders that are used, so we should probably explore supporting them as well. |
In addition to changes for #26721 to have constructor initialization built into This does not cover multiple constructors and builders that @rwinch mentioned. That is related but should be considered separately I think. |
Hans Desmet opened SPR-16259 and commented
Since Spring 5, the
DataBinder
can use the parametrized constructor to populate the command object upon form submission, which is a great improvement.It would be nice and handy if the
DataBinder
could also use the parametrized constructor on nested objects of the command object.When using following class for the command object:
which has following class for the nested object:
upon form submission an Exception occurs:
Affects: 5.0.2
The text was updated successfully, but these errors were encountered: