如何为 YAML 对象中的几个键设置一个值

how to set one value for few key's in YAML object

据我所知,在 YAML 中无法将少数键附加到 {key : value} 对中的单个值。 我是 Swagger 文档 (OpenAPI 3.0) 的新手,想知道是否有可能以某种方式将我的值组合在一起。 例如,我在搞乱 REST API 文档,当我想列出一些响应而不是每次都重写时,我有以下内容:

paths:
  /users:
      post:
//some additional information here
        responses:
          '500':
//and for example here I want to add not only 201 status code but also others
                description: >-
                  Something went wrong on back end
//however I have to do something like this (writing them all)
          '502':
                description: >-
                  Something went wrong on back end
//and so on..

克服这个问题的最佳做法是什么?可能我一开始就说错了

您可以使用锚点和别名:

path:
  /users:
    responses:
      '500':
        description: &desc1 >-
          Something went wrong on back end
      '502':
        description: *desc1

OpenAPI 3.0 支持响应范围 '1XX'-'5XX' 可以使用而不是单独列出代码:

    responses:
      '5XX':
        description: Something went wrong on back end

如果您仍想单独列出响应代码,但它们具有相同的 descriptionschema,您可以 $ref 通用响应定义,如下所示:

    responses:
      '501':
        $ref: '#/components/schemas/BackendErrorResponse'
      '502':
        $ref: '#/components/schemas/BackendErrorResponse'

components:
  responses:
    BackendErrorResponse:
      description: Something went wrong on back end
      content:
        application/json:
          schema:
            ...

OpenAPI 规范存储库中还有一个增强请求,以允许 reusing description across different responses