Closed
Description
Kazuki Shimizu opened SPR-15542 and commented
I've tried the the request data binding feature for immutable object. It's work fine. However binding error(such as type conversion error) can not handle using the BindingResult
as follow:
package com.example;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;
@RestController
public class ImmutableObjectRestController {
@GetMapping("/immutable")
Query search(@Validated Query query, BindingResult bindingResult) {
System.out.println(bindingResult);
return query;
}
public static class Query {
@NotNull private final String name;
private final String mail;
private final String tel;
private final LocalDate baseDate;
public Query(String name, String mail, String tel,
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate baseDate) {
this.name = name;
this.mail = mail;
this.tel = tel;
this.baseDate = baseDate;
}
public String getName() {
return name;
}
public String getMail() {
return mail;
}
public String getTel() {
return tel;
}
public LocalDate getBaseDate() {
return baseDate;
}
}
}
A validation error(e.g. name
parameter is null
) can handle above implementation.
$ curl -D - http://localhost:8080/[email protected]\&tel=09012345678\&baseDate=2017-08-01
print on console as follow:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'query' on field 'name': rejected value [null]; codes [NotNull.query.name,NotNull.name,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [query.name,name]; arguments
However a binding error(e.g. baseDate
parameter is nonexistence date) can not handle above implementation.
$ curl -D - http://localhost:8080/[email protected]\&tel=09012345678\&baseDate=2017-08-32
HTTP/1.1 400
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 14 May 2017 11:20:29 GMT
Connection: close
{"timestamp":"2017-05-14T11:20:29.495+0000","status":400,"error":"Bad Request","message":"Failed to convert value of type 'java.lang.String[]' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDate] for value '2017-08-32'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2017-08-32]","path":"/https/github.com/immutable"}
Is specification this behavior ?
Affects: 5.0 RC1
Reference URL: #19763
Issue Links:
- Data binding with immutable objects (Kotlin / Lombok / @ConstructorProperties) [SPR-15199] #19763 Data binding with immutable objects (Kotlin / Lombok /
@ConstructorProperties
) - Kotlin class instantiation with optional parameters and default values [SPR-15673] #20232 Kotlin class instantiation with optional parameters and default values
- Cannot create BindStatus for valid field on immutable form object in case of bind errors [SPR-16449] #20994 Cannot create BindStatus for valid field on immutable form object in case of bind errors
- Revisit handling of missing fields (without default values) for immutable data classes [SPR-15877] #20432 Revisit handling of missing fields (without default values) for immutable data classes
- Immutable object constructor arguments not considering WebDataBinder's FIELD_MARKER_PREFIX [SPR-15871] #20426 Immutable object constructor arguments not considering WebDataBinder's FIELD_MARKER_PREFIX
Referenced from: commits 61cdc84