使用 $ref 时忽略 Swagger 模式属性 - 为什么?

mvc*_*mvc 18 swagger swagger-2.0 openapi

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

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"        
Run Code Online (Sandbox Code Playgroud)

出于某种原因,生成的 HTML 没有显示lowerBound 和upperBound“描述”,而只显示原始时间“描述”。这让我觉得我没有正确地做这件事。

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

Hel*_*len 18

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

开放API 3.1

迁移到 OpenAPI 3.1 后,您的定义将按预期工作。这个新版本与 JSON Schema 2020-12 完全兼容,它允许schemas 中的$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"  
Run Code Online (Sandbox Code Playgroud)

在模式之外- 例如,在响应或参数中 - $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
Run Code Online (Sandbox Code Playgroud)

OpenAPI 2.0 和 3.0.x

在这些版本中,$ref通过用它指向的定义替换它自己和它的所有兄弟元素来工作。这就是为什么

      lowerBound:
        $ref: "#/definitions/Time"
        description: Lower bound on the time interval.
        default: "00:00"
Run Code Online (Sandbox Code Playgroud)

变成

      lowerBound:
        type: string
        description: Time in 24 hour format "hh:mm".
Run Code Online (Sandbox Code Playgroud)

可能的解决方法是包装$refallOf-这可以用来“添加”属性的$ref,但是没有覆盖现有属性。

      lowerBound:
        allOf:
          - $ref: "#/definitions/Time"
        description: Lower bound on the time interval.
        default: "00:00"
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用$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"
Run Code Online (Sandbox Code Playgroud)

  • @Helen,您能否提供 Open API 的官方文档链接,说明官方支持在兄弟姐妹旁边使用 $ref ?根据官方文档,使用 Open API [此处](https://swagger.io/docs/specation/using-ref/#sibling) 不支持在 $ref 旁边使用同级。 (2认同)