【问题标题】:Swagger schema properties ignored when using $ref - why?使用 $ref 时忽略 Swagger 模式属性 - 为什么?
【发布时间】:2016-02-11 07:35:33
【问题描述】:

我正在尝试建立一个时间间隔的Swagger模型,使用一个简单的字符串来存储时间(我知道还有datetime):

definitions:
  Time:
    type: string
    description: Time in 24 hour format "hh:mm".
  TimeInterval:
    type: object
    properties:
      lowerBound:
        $ref: "#/definitions/Time"
        description: Lower bound on the time interval.
        default: "00:00"
      upperBound:
        $ref: "#/definitions/Time"
        description: Upper bound on the time interval.
        default: "24:00"        

由于某种原因,生成的 HTML 不显示 lowerBound 和 upperBound “描述”,而只显示原始 Time “描述”。这让我觉得我没有正确地做到这一点。

所以问题是,使用模型作为类型实际上是否可以像我尝试的那样完成。

【问题讨论】:

    标签: swagger openapi swagger-2.0


    【解决方案1】:

    TL;DR:OpenAPI 3.1 支持$ref 兄弟姐妹。在以前的 OpenAPI 版本中,$ref 旁边的任何关键字都会被忽略。

    OpenAPI 3.1

    迁移到 OpenAPI 3.1 后,您的定义将按预期工作。这个新版本与 JSON Schema 2020-12 完全兼容,允许$ref 兄弟姐妹在模式中

    openapi: 3.1.0
    ...
    
    components:
      schemas:
        Time:
          type: string
          description: Time in 24 hour format "hh:mm".
    
        TimeInterval:
          type: object
          properties:
            lowerBound:
              # ------- This will work in OAS 3.1 ------- #
              $ref: "#/components/schemas/Time"
              description: Lower bound on the time interval.
              default: "00:00"
            upperBound:
              # ------- This will work in OAS 3.1 ------- #
              $ref: "#/components/schemas/Time"
              description: Upper bound on the time interval.
              default: "24:00"  
    

    模式之外 - 例如,在响应或参数中 - $refs 只允许同级 summarydescription 关键字。这些 $ref 旁边的任何其他关键字都将被忽略。

    # openapi: 3.1.0
    
    # This is supported
    parameters:
      - $ref: '#/components/parameters/id'
        description: Entity ID
    
    # This is NOT supported
    parameters:
      - $ref: '#/components/parameters/id'
        required: true
    

    OpenAPI 2.0 和 3.0.x

    在这些版本中,$ref 的工作原理是将自身和其所有同级元素替换为它所指向的定义。这就是为什么

          lowerBound:
            $ref: "#/definitions/Time"
            description: Lower bound on the time interval.
            default: "00:00"
    

    变成

          lowerBound:
            type: string
            description: Time in 24 hour format "hh:mm".
    

    一种可能的解决方法是将$ref 包装到allOf - 这可用于将属性“添加”到$ref,但不能覆盖现有属性。

          lowerBound:
            allOf:
              - $ref: "#/definitions/Time"
            description: Lower bound on the time interval.
            default: "00:00"
    

    另一种方法是使用内联定义替换$ref

    definitions:
      TimeInterval:
        type: object
        properties:
          lowerBound:
            type: string  # <------
            description: Lower bound on the time interval, using 24 hour format "hh:mm".
            default: "00:00"
          upperBound:
            type: string  # <------
            description: Upper bound on the time interval, using 24 hour format "hh:mm".
            default: "24:00"
    

    【讨论】:

    • 如果此示例中的 TimeInterval 是生成 swagger 定义的类——该类也可能具有 Swagger 注释——你知道如何对类和/或注释进行编码,以便它会在这个答案中生成任何一种解决方法(即使用allOf,或者使用内联定义而不是$ref)?
    • @ChrisW 我不知道,抱歉。尝试asking a new question 关于注释。
    • 谢谢。已经有这样一个问题(here),不知道你是不是碰巧知道。我想我会尝试通过(而不是注释)为该类构造一个模型实例并通过 ModelResolver 注入该自定义模型实例来实现它。
    • @Helen,你能提供 Open API 的官方文档链接,说明官方支持在兄弟姐妹旁边使用 $ref 吗?根据官方文档,Open API here 不支持在 $ref 旁边使用兄弟姐妹。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多