【问题标题】:Different examples based on inheritance with Open API 3基于 Open API 3 继承的不同示例
【发布时间】:2020-11-11 14:33:03
【问题描述】:

我想在 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

【问题讨论】:

    标签: spring-boot inheritance springdoc


    【解决方案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'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-26
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      相关资源
      最近更新 更多