payne
payne

Reputation: 5289

Provide default value to Swagger DTO object

When reading about @ApiModelProperty, I thought I had finally found how to solve this, but it just didn't work.

Here is what I'm working with:

Controller

@RestController
@Api(value = "inventorySnapshot")
@RequestMapping("/business/v1/inventorySnapshots")
@Slf4j
public class InventorySnapshotController  {

    @ApiOperation(value = "@api.operation.summary.put_dtos@")
    @PutMapping
    public ResponseEntity<Void> put(final @RequestBody List<MyDTO> dtos) {
        log.debug("Put InventorySnapshots");
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}

DTO

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MyDTO {

    @NotNull(groups = ForDocumentationOnly.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", lenient = OptBoolean.FALSE)
    private Date availableFromDate;

    @ApiModelProperty(example = "2021-01-11T11:11:11Z")
    @NotNull(groups = ForDocumentationOnly.class)
    @DateTimeFormat(pattern = "yyyy-MM-ddThh:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'", lenient = OptBoolean.FALSE)
    private Timestamp calculationDateTime;

    // more properties for which I wish to keep the default suggested by Swagger...
}

Result

To change 1

To change 2

Expected

I'm rather looking forward to having it like so:

enter image description here

Any clues ?

Upvotes: 2

Views: 2647

Answers (1)

Ravi
Ravi

Reputation: 180

You can use JSON INPUT for the default value.

example:

public ResponseEntity<Void> put(
  @RequestBody 
  @ApiParam(defaultValue="\\"availableFromDate\\":\\"2021-10-28\\",\\"calculationDateTime\\":\\"2021-10-28T01:35:46Z\\"")
  MyDTO dto)

or if you are using open api use below

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema
public class MyDTO {

  @NotNull(groups = ForDocumentationOnly.class)
  @DateTimeFormat(pattern = "yyyy-MM-dd")
  @JsonFormat(pattern = "yyyy-MM-dd", lenient = OptBoolean.FALSE)
  @Schema(example="2021-01-11")
  private Date availableFromDate;

  @Schema(example = "2021-01-11T11:11:11Z")
  @NotNull(groups = ForDocumentationOnly.class)
  @DateTimeFormat(pattern = "yyyy-MM-ddThh:mm:ss")
  @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'", lenient = OptBoolean.FALSE)
  private Timestamp calculationDateTime;

  // more properties for which I wish to keep the default suggested by Swagger...
}

Upvotes: 3

Related Questions