I have the following DTO which uses the @Parameter annotation to mark the first field as "required":
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.NotNull;
@Schema(description = "My DTO")
class VectorFoo {
// The below attribute is required
@NotNull
@Parameter(description = "my first attribute", required = true)
private String attribute1;
// The below attribute is optional
@Parameter(description = "my second attribute", required = false)
private String attribute2;
public String getAttribute1() {
return attribute1;
}
public void setAttribute1(String attribute1) {
this.attribute1 = attribute1;
}
public String getAttribute2() {
return attribute2;
}
public void setAttribute2(String attribute2) {
this.attribute2 = attribute2;
}
}
And the following REST Endpoint code:
@PostMapping("/fstVector")
public ResponseEntity<VectorFoo> fstVPost(
// RequestBody objects are "required" by default. To make them optional, add "(required = false)"
@org.springframework.web.bind.annotation.RequestBody // Spring
@io.swagger.v3.oas.annotations.parameters.RequestBody // Swagger
@Valid // Bean validation to ensure if the incoming object is valid
final VectorFoo v1
) {
return new ResponseEntity<>(v1, HttpStatus.OK);
}
Which produces the following Swagger UI. Note the lack of "required" notation on on any of the fields the Request Body section.
I understand from this StackOverflow post that this should have worked to show "required" in the Swagger UI for the Request Body example (see final answer with Swagger UI Screenshots): Post about Swagger UI and "required" fields in RequestBody
Can anyone help me understand how I can get Swagger to show the required fields that have been marked as "required" (on the DTO with the @Parameter annotation) in the Request Body documentation on the Swagger UI?
Thanks.