Affects: 3.1.4

In a REST method, when using a POJO with @RequestBody and @Valid, and within that POJO having an optional field with the type tagged with e.g. @NotBlank (e.g.Optional<@NotBlank String>), a MethodArgumentNotValidException is triggered when the field is not defined in the sent JSON. This behavior is unexpected, as an omitted field should be considered valid, and the @NotBlank validation should only be applied if the field is defined and contains a value.

Steps to reproduce

  • Create a Spring Boot application.
  • Define a REST endpoint that accepts a POJO as a request body.
  • Annotate the POJO with @Valid
  • The POJO should have an optional field with the type annotated with e.g. @NotBlank, for example: Optional<@NotBlank String>
  • Send a JSON request that omits the optional field.

Expected Behavior:

The omitted field of an optional value (in the JSON), resulting in an empty optional (in the POJO) should be considered valid, and the @NotBlank validation should only be triggered if the field is defined in the JSON request containing a value.

Actual Behavior:

A MethodArgumentNotValidException is thrown when the optional field (in the JSON) is omitted, even though it should be considered valid in this case.

Environment:

  • Spring Framework version: 3.1.4
  • Java version: 20
  • Operating System: Ubuntu 20.04

Example

@RestController
public class MyController {
    @PostMapping("/my-endpoint")
    public ResponseEntity<String> myEndpoint(@Valid @RequestBody MyPojo myPojo) {
        // ...
    }
}

public class MyPojo {
    @NotBlank
    private String requiredField;

    private Optional<@NotBlank String> optionalField;

    // Getters and setters...
}

{
  "requiredField": "fieldData"
}

Sending this JSON to the endpoint causes a MethodArgumentNotValidException, which is unexpected behavior. This is causing inconvenience when working with REST endpoints where certain fields are optional but need to be validated when provided. The current behavior makes it difficult to handle optional fields in a user-friendly manner.

Comment From: snicoll

The omitted optional field should be considered valid, and the @NotBlank validation should only be triggered if the field is defined in the JSON request and contains a value.

I don't really understand why you're expecting that. The support for Optional is the transparent unwrapping of the container (see also the hibernate validator doc). It's not a way to convey that the validation is optional.

Comment From: NikolasFunction

Thanks for your response.

Maybe my description was a bit misleading. Let me clarify my "Expected behaviour":

The omitted field of an optional value (in the JSON), resulting in an empty optional (in the POJO) should be considered valid, and the @NotBlank validation should only be triggered if the field is defined in the JSON request containing a value.

Actual Behavior:

A MethodArgumentNotValidException is thrown when the optional field (in the JSON) is omitted, even though it should be considered valid in this case.

Does this clarification make it clearer?