0

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.

enter image description here

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.

jb62
  • 2,224
  • 2
  • 14
  • 23
  • Please ask an actual and specific answerable question. Put some text in the question that gives the details of the code and the problem. – Hovercraft Full Of Eels Jun 20 '23 at 23:21
  • If you replace `@Parameter(...)` with `@Schema(...)`, does it resolve the issue? – Helen Jun 21 '23 at 08:01
  • And no, unfortunately although I tried @Schema(description = "my first attribute", required = true) it still did not mark the RequestBody field "attribute1" as "required". – jb62 Jun 21 '23 at 16:01
  • @Helen - I accidentally entered this question (due to misunderstanding how to use the SO UI) without details and SO moderators deleted my "real" question. I have updated this one now - and attempted your suggestion with "Schema" - which didn't work, unfortunately. Do you have any other suggestions for me? Thanks! – jb62 Jun 22 '23 at 16:12
  • You wrote _'Note the lack of "required" notation on on any of the fields the Request Body section.'_ If you switch to the "Schema" tab in the "Request body" section, does the info there indicate the required properties? I'm asking because the required field are only indicated in the "Schema" view and not in the "Example Value" view - see https://i.stack.imgur.com/gxlsY.png – Helen Jun 22 '23 at 21:23
  • I'm not super familiar with Swagger and have only ever used annotations in Java code. I'm guessing that the "Schema" tab you mention is in some tool that is associated with Swagger in some way? If so - we don't have that in our apps/services. – jb62 Jun 22 '23 at 21:26
  • If this is something that is generated by Swagger and available somewhere in a project, I'd be grateful for a hint on where to look for it. Thanks! – jb62 Jun 22 '23 at 21:41
  • The "Schema" tab is part of the "Request body" section in Swagger UI, check the link to the right of the "Example Value" caption, as indicated on the screenshot: https://i.stack.imgur.com/gxlsY.png. The required properties are marked with `*` there. – Helen Jun 25 '23 at 20:44
  • 1
    Ha! It was there all along... So the user has to know that the word "Schema" is a link and that clicking on it will show which fields are required. I was unaware of that - as are most/all of my API endpoint consumers. I will find a way to let them know. Thanks! – jb62 Jun 26 '23 at 16:12
  • @Helen - if you want to drop a quick "answer" in here, I'll vote it up. Thanks for helping me learn this about Swagger UI. – jb62 Jun 26 '23 at 16:13

0 Answers0