Search code examples
javaspringvalidationspring-restdocs

Why do Spring ResDocs FieldDescriptor don't gather constraint on inner objects?


currently I'm struggling with a FieldDescriptor which seem not to "see" all validation constraints. Following scenario:

I have an Dto similar to:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExampleDto {

    @NotNull
    @Valid
    private GeoCoordinates coordinate;

    @Min(value = 0)
    @Max(value = 100)
    private BigDecimal workingProperty = BigDecimal.valueOf(0);

//...

And a class GeoCoordinates like this:

@Getter
@NoArgsConstructor
public class GeoCoordinates {

    @NotNull
    @Max(value = 180)
    private Double latitude;

//...

The constraints on the "workingProperty" are properly found on the FieldDescriptor: enter image description here

The opposite is happening on the FieldDescriptor of the coordinate: enter image description here

Following the documentation https://docs.spring.io/spring-restdocs/docs/current/reference/html5/#documenting-your-api-constraints-finding I don't see the issue why the constraint on one field is found and not on the other. Especially as the Validator is validating everything correctly.


Solution

  • After some research I found the reason for the issue in a custom ValidatorConstraintResolver. It was only investigating the top level constraints and i had to dive deeper by using reflections.