Skip to content

Ignore @NotNull annotations #4213

@nvervelle

Description

@nvervelle

In ModelResolver, the list NOT_NULL_ANNOTATIONS is defined and contains the annotations that implies that the field is required.

Is there a way to disable this behavior, or remove some annotations from the list ? Or to ignore the annotation on some attributes ?

The assumption that a field annotated @NotNull should always be declared as required = true is incorrect and we should be able to disable this behavior. It can cause problem both for incoming data structure and for outgoing data structure.

Example of problems for incoming data structure

    @Schema(description = "Additional properties")
    @JsonProperty("additionalProperties")
    @JsonSetter(nulls = Nulls.AS_EMPTY)
    @NotNull
    Map<String, String> additionalProperties

The parameter additionalProperties should be marked as required = false, even with the @NotNull annotation : when the additionalProperties attribute is not provided, the @JsonSetter annotation will ensure that the parameter is properly initialized with an empty Map. So on the Java side, it's clearly a non-nullable attribute, but on the openapi spec side it should be required = false.

Example of problems for outgoing data structure

    @Schema(description = "Additional properties")
    @JsonProperty("additionalProperties")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @NotNull
    Map<String, String> additionalProperties

The parameter additionalProperties should be marked as required = false, even with the @NotNull annotation : when the additionalProperties parameter is an empty map, the @JsonInclude annotation will ensure that the attribute is not added to the payload. So on the Java side, it's a non-nullable attribute, but on the openapi spec side it should be required = false.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions