Closed
Description
Affects: Spring 6.1.5 with Spring Boot 3.1.5
I have this class
@NoArgsConstructor
public class RequestDTO {
@NotNull
private String name;
@NotNull
private LocalDate startDate;
@NotNull
private LocalDate endDate;
@JsonIgnore
@AssertTrue(message = "Range not valid")
public boolean isRangeValid() {
if(getEndDate()==null) return true;
if(getStartDate()==null) return true;
if (getStartDate().isEqual(getEndDate())) return true;
return getEndDate().isAfter(getStartDate());
}
}
and a subclass:
@EqualsAndHashCode(callSuper = true)
@JsonIgnoreProperties("code")
public class RequestExtensionDTO extends RequestDTO{
public RequestExtensionDTO(){
super();
}
}
When I use the subclass with @Valid
in a POST method of controller...
@PostMapping("/simplePostChilDTO")
public ResponseEntity<?> simplePostChildDTO(@RequestBody
@Valid RequestExtensionDTO template) {
return ResponseEntity.ok().build();
}
... the annotations defined in the parent class do not trigger; @AssertTrue
defined on the method instead triggers.
The following metadata is produced:
{
"name":"test.model.RequestDTO",
"methods":[
{
"name":"getStartDate",
"parameterTypes":[
]
},
{
"name":"getEndDate",
"parameterTypes":[
]
},
{
"name":"setName",
"parameterTypes":[
"java.lang.String"
]
},
{
"name":"setStartDate",
"parameterTypes":[
"java.time.LocalDate"
]
},
{
"name":"isRangeValido",
"parameterTypes":[
]
},
{
"name":"setEndDate",
"parameterTypes":[
"java.time.LocalDate"
]
},
{
"name":"getName",
"parameterTypes":[
]
}
]
}
In order to overcome the problem I have to create a specific hint for the parent class:
hints.reflection().registerType(RequestDTO.class, MemberCategory.values());
This way the constraints are all triggered.
Is this behavior correct?
If you need it, I have a demo.