具有通用类型的@APIResponse

时间:2019-07-16 14:08:44

标签: java api documentation openapi microprofile

我正在构建一个API,并且所有列表操作都返回我从Paged调用的同一对象:

public class Paged<T> {

  public static <J> Paged<J> of(int offset, int limit, long total, List<J> results) {
    return new Paged<>(offset, limit, total, results);
  }

  public Paged() {
  }

  private Paged(int offset, int limit, long total, List<T> results) {
    this.offset = offset;
    this.limit = limit;
    this.total = total;
    this.results = results;
  }

  private int offset;
  private int limit;
  private long total;
  private List<T> results;

  public int getOffset() {
    return offset;
  }

  public void setOffset(int offset) {
    this.offset = offset;
  }

  public int getLimit() {
    return limit;
  }

  public void setLimit(int limit) {
    this.limit = limit;
  }

  public long getTotal() {
    return total;
  }

  public void setTotal(long total) {
    this.total = total;
  }

  public List<T> getResults() {
    return results;
  }

  public void setResults(List<T> results) {
    this.results = results;
  }
}

如何告诉Microprofile OpenApi来自Paged的响应是User的列表?

@Operation(summary = "List all users", description = "List all users")
@APIResponse(responseCode = "200", description = "All Users", content = { @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = Paged.class, type = SchemaType.OBJECT)) })

使用上面的注释,我有以下文档:

content:
  application/json:
    schema:
      type: object
      properties:
        limit:
          format: int32
          type: integer
        offset:
          format: int32
          type: integer
        results:
          type: array
          items:
            type: object
        total:
          format: int64
          type: integer

当我期待类似的东西时:

content:
  application/json:
    schema:
      type: object
      properties:
        limit:
          format: int32
          type: integer
        offset:
          format: int32
          type: integer
        results:
          type: array
          items:
            $ref: '#/components/schemas/User'
        total:
          format: int64
          type: integer

0 个答案:

没有答案