The springdoc-openapi library automatically marks certain properties as required in the generated OpenAPI documentation. For instance, properties annotated as @NotNull
will be included in the list of required properties in the generated YAML file.
One thing the library does not do is mark optional properties as nullable: true
. However, by default a Spring Boot application will accept null
in requests and return null
in responses for optional properties. This means that there is a discrepancy between the OpenAPI documentation and the behavior of the endpoint.
It is trivial to manually mark any individual property as nullable: just add @Schema(nullable = true)
to the field or accessor. However, in a large model with multiple properties, I would rather this be automatically determined in the same manner as the required
property. Namely, if the property is not required, I would want it to be nullable
, and vice versa.
How can I get my optional properties marked as nullable: true
in OpenAPI documentation generated by springdoc-openapi?
Example
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.NotNull;
public class RequiredExample {
@NotNull
private String key;
private String value;
public String getKey() { return key; }
public void setKey(String key) { this.key = key; }
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
Generated OpenAPI documentation:
"components": {
"schemas": {
"RequiredExample": {
"required": [
"key"
],
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
}
}
Desired OpenAPI documentation:
"components": {
"schemas": {
"RequiredExample": {
"required": [
"key"
],
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
"nullable": true
}
}
}
}
}