Disable JSR-303 annotation processing in springdoc

How can I disable JSR-303 annotation processing in springdoc for specific fields?

I have the following request class MyRequestTO where the field name is actually optional. The @NotBlank annotation is only applied to the unwrapped JsonNullable. This means the user is allowed to omit the field when sending MyRequestTO but if set it must not be blank. However the open api doc marks the name field as required. Changing the @Schema annotation to @Schema(type = "string", required = false) does not help.

I want to avoid a solution where I have to write my own annotation and make use of org.springdoc.core.customizers.OpenApiCustomiser. The desired solution should also work for other types like JsonNullable<Boolean> annotated with @NotNull.

public class MyRequestTO {

    @Schema(type = "string")
    @NotBlank
    private JsonNullable<String> name = JsonNullable.undefined();

    public JsonNullable<String> getName() {
        return name;
    }

    public void setName(JsonNullable<String> name) {
        this.name = name;
    }
}

Relevant dependencies

implementation "org.openapitools:jackson-databind-nullable:0.2.1"
implementation "org.springdoc:springdoc-openapi-ui:1.5.5"

Answer

@crizzis solution from the comments to my question works as expected. Fields are no longer marked as required but if supplied have to conform to the annotation constraints.

The JSR-303 (e.g. @NotBlank or @NotNull) annotation belongs in front of the type parameter:

private JsonNullable<@NotBlank String> name = JsonNullable.undefined();
private JsonNullable<@NotNull Boolean> enabled = JsonNullable.undefined();

Then the resulting openAPI docs will mark the fields as "required" : false.

Leave a Reply

Your email address will not be published. Required fields are marked *