Search code examples
javaswaggeropenapi

OpenAPI 3.0 how to show dto fields as query parameters


I am migrating from Swagger 2.x to OpenAPI 3.0 and encountered some sort of troubles: I was using ApiModel with ApiModelProperties to show dto fields as parameters to a method, but now it only shows up as a JSON object with no field descriptions. Now it looks like: enter image description here But i want it: enter image description here

@RequestMapping("/smth")
@RequiredArgsConstructor
@RestController
public class Controller {
  @GetMapping
  @ResponseStatus(HttpStatus.OK)
  @Operation(summary = "Filtered values")
  public List<GD> findAll(Parameter(explode = Explode.TRUE) final PageFilter pageFilter) {
    return baseAdapter.findAll(pageFilter);
  }

Dto:

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@Schema(type = "object")
public class PageFilter {
  @Getter(AccessLevel.NONE)
  @Setter(AccessLevel.NONE)
  private static final String DELIMITER = ";";

  @Schema(
      name = "page",
      description = "Results page you want to retrieve (0..N)",
      example = DEFAULT_PAGE_STRING,
      type = "integer")
  private Integer page;

  @Schema(
      description = "Number of records per page",
      example = DEFAULT_SIZE_STRING,
      type = "integer")
  private Integer size;

  @Schema(
      description =
          "Sorting criteria in the format: property(propertyName;asc|desc). "
              + "Default sort order is ascending. Multiple sort criteria are supported.")
  private String[] sort;

}

Solution

  • Add:

    • @Parameter annotation on the fields of your Dto object, and
    • @ParameterObject annotation on your controller's parameter

    Credit to @Shamil Mukhetdinov for the fix! (the answer is in the comments of the issue.)