首页 > 解决方案 > OpenAPI:覆盖引用,引用单个字段

问题描述

我正在使用两个文件:

common.yaml包含:

components:

  schemas:
    GenericError:
      type: object
      properties:
        code:
          description: The error code.
          type: integer
        message:
          description: The error description.
          type: string
        details:
          description: Optional details.
          type: object

  examples:
    Bamboozled:
      value:
        code: 10
        message: Bamboozled
        details: You've been bamboozled

services.yaml` 包含:

paths:
  /logistic:
    post:

      description: Logistic

      responses:
        403:
          description: Forbidden
          content:
            application/json:
              schema:
                $ref: '../common.yaml#/components/schemas/GenericError'
              examples:
                Bamboozled:
                  value:
                    $ref: '../common.yaml#/components/examples/Bamboozled/value'

services.yaml中,我想覆盖该details字段。我尝试了以下方法:

              examples:
                Bamboozled:
                  value:
                    $ref: '../common.yaml#/components/examples/Bamboozled/value'
                    details: my custom details

但这不起作用,它会呈现:

{
  "$ref":

  "#/paths/~1logistic/post/responses/403/content/application~1json/examples/Bamboozled/value",
"details": "my custom details"
}

有没有办法可以正确覆盖它?

或者,我可以仅引用特定字段吗?所以我会有services.yaml一些东西,比如:

              examples:
                Bamboozled:
                  value:
                    $ref: '../common.yaml#/components/examples/Bamboozled/value/code'
                    $ref: '../common.yaml#/components/examples/Bamboozled/value/message'
                    details: my custom details

(这不起作用)

标签: yamlswaggeropenapi

解决方案


推荐阅读