swagger 通过OpenAPI 3定义Map时添加示例键

siv3szwd  于 2023-05-06  发布在  其他
关注(0)|答案(1)|浏览(321)

这是我在响应中使用的JSON格式:

{
    "status": "success",
    "error": null,
    "data": {
        "<product_id>": {
            "productDetails": {
                "name": "Name of product",
                "currency": "USD",
                ...
            },
            ...
        }
    }
}

在定义产品Map时,我在OpenAPI 3中添加了以下内容。

components:
  schemas:
    ApiResponse:
      type: object
      properties:
        status:
          type: string
          default: success
          enum:
            - success
            - failure
        error:
          type: string
          example: "Internal error"
        data:
          type: object
          additionalProperties:
            $ref: '#/components/schemas/ProductData'
    ProductData:
      type: object
      properties:
        productDetails:
          type: object
          $ref: '#/components/schemas/ProductDetails'
    ProductDetails:
      type: object
      properties:
        name:
          type: string
          example: "Sample prod name"
        currency:
          type: string
          example: "USD"

在swagger中,这将生成如下示例:

{
  "status": "success",
  "error": "Internal error",
  "data": {
    "additionalProp1": {
      "productDetails": {
        "name": "Sample prod name",
        "currency": "USD"
      }
    },
    "additionalProp2": {
      "productDetails": {
        "name": "Sample prod name",
        "currency": "USD"
      }
    },
    "additionalProp3": {
      "productDetails": {
        "name": "Sample prod name",
        "currency": "USD"
      }
    }
  }
}

然而,我想传递一个自定义示例作为密钥名称(比如,“d8467030-e737- 11 ed-a05 b-0242 ac 120003”),而不是 swagger 地填充密钥(如“additionalProp 1”,“additionalProp 2”等)。这怎么可能?
我从swagger检查了OpenAPI 3.0 documentation,但找不到这样做的方法。

bpsygsoo

bpsygsoo1#

在OpenAPI v3.1中,您可以使用propertyNames关键字-https://json-schema.org/understanding-json-schema/reference/object.html#property-names

相关问题