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?
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; }
}
"components": {
"schemas": {
"RequiredExample": {
"required": [
"key"
],
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
}
}
"components": {
"schemas": {
"RequiredExample": {
"required": [
"key"
],
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
"nullable": true
}
}
}
}
}
You could directly annotate your attribute with @Schema. The key here is that both nullable and requiredMode will have to bet set.
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.NotNull;
public class RequiredExample {
@NotNull
private String key;
@Schema(nullable = true, requiredMode = Schema.RequiredMode.REQUIRED)
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; }
}
For some context, this was my exact use case.
One solution is to create a springdoc-openapi OpenApiCustomiser Spring bean which sets all properties to nullable unless they are in the list of required properties. This approach benefits from the built-in springdoc-openapi support for @NotNull and other such annotations, since the required property will have been calculated in the standard manner based on the presence of such properties.
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import org.springdoc.core.customizers.OpenApiCustomiser;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class NullableIfNotRequiredOpenApiCustomizer implements OpenApiCustomiser {
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public void customise(OpenAPI openApi) {
for (Schema schema : openApi.getComponents().getSchemas().values()) {
if (schema.getProperties() == null) {
continue;
}
((Map<String, Schema>) schema.getProperties()).forEach((String name, Schema value) -> {
if (schema.getRequired() == null || !schema.getRequired().contains(name)) {
value.setNullable(true);
}
});
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With