springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.29k stars 500 forks source link

Consider fields in response schemas as required #2402

Closed bopi- closed 1 year ago

bopi- commented 1 year ago

Describe the solution you'd like

Spring uses Jackson to serialize the java objects to json. Since Jackson serializes all fields by default the generated OpenApi documentation would match that behavior.

Optional fields could be marked by @Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED) or even better by @JsonInclude(JsonInclude.Include.NON_NULL) and be generated by springdoc as not required

Describe alternatives you've considered

Annotate every field with @Schema(requiredMode = Schema.RequiredMode.REQUIRED)

bnasslahsen commented 1 year ago

@bopi-,

Fields in Schema are not considered as required by default. If you add @Schemaannotation, they would be marked as required.

bopi- commented 1 year ago

Hi @bnasslahsen,

thank you for your fast response.

Exactly. That is why I created this feature request. From my point of view it would be great if springdoc would consider fields in response schemas as required by default. Jackson serializes all fields by default so in fact they are required by default.