0

我想在 Swagger 中为父属性制作不同的示例。有什么简单的方法可以实现这一目标吗?

class Link {
    @Schema(description = "Next link", example = "http://")
    private String next;
    @Schema(description = "Previous link", example = "http://")
    private String previous;
}
class Sample<T> {
    private List<T> items;
    private Link _href;
}
class SampleA extends Sample<A> {}
class SampleB extends Sample<B> {}

SampleA 的示例是

  • 下一个:abc.com
  • 上一个:abc.com?page=2

SampleB 的例子是

  • 下一个:xyz.com
  • 上一篇:xyz.com?page=2
4

1 回答 1

1

这是继承的示例工作代码:

class Link {
    @Schema(description = "Next link", example = "http://")
    @JsonProperty
    private String next;

    @Schema(description = "Previous link", example = "http://")
    @JsonProperty
    private String previous;
}

@Schema(subTypes = {SampleA.class, SampleB.class})
class Sample<T> {
    @JsonProperty
    private List<T> items;
    @JsonProperty
    private Link _href;
}

@Schema(allOf = Sample.class)
class SampleA extends Sample {}

@Schema(allOf = Sample.class)
class SampleB extends Sample {}


@RestController
public class HelloController {

    @GetMapping("/getA")
    SampleA getA() {
        return new SampleA();
    }

    @GetMapping("/getB")
    SampleB getAB() {
        return new SampleB();
    }
}

这是由此产生的 OpenAPI 规范:

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: 'http://localhost:8080'
    description: Generated server url
paths:
  /getA:
    get:
      tags:
        - hello-controller
      operationId: getA
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/SampleA'
  /getB:
    get:
      tags:
        - hello-controller
      operationId: getAB
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/SampleB'
components:
  schemas:
    Link:
      type: object
      properties:
        next:
          type: string
          description: Next link
          example: 'http://'
        previous:
          type: string
          description: Previous link
          example: 'http://'
    Sample:
      type: object
      properties:
        items:
          type: array
          items:
            type: object
        _href:
          $ref: '#/components/schemas/Link'
    SampleA:
      type: object
      allOf:
        - $ref: '#/components/schemas/Sample'
    SampleB:
      type: object
      allOf:
        - $ref: '#/components/schemas/Sample'
于 2020-08-02T15:13:29.130 回答