如何创建招摇阵列

How to create swagger array

我正在尝试为以下 JSON 创建一个 swagger 文档,但我收到以下错误:schemas with 'type: array',require a sibling 'items: ' 字段

JSON:

{
    "_id": "string",
    "name": "string",
    "descriptions": {},
    "date": "string",
    "customer": {
        "id": "string",
        "name": {
            "firstName": "string",
            "lastName": "string",
            "middleName": "string"
        }
    },
    "productDetials": {
        "id": "string",
        "name": {
            "name": "string",
            "model": "string",
            "price": "string",
            "comments": "string"
        }
    },
    "Phone": [{
            "id": "string",
            "category": "string",
            "version": "string",
            "condition": "string",
            "availability": "string"

        }
    ]   
}

谁能帮我得到这个 JSON 的 swagger 文档。

任何帮助将不胜感激。

首先你必须定义依赖于JSON(对象)的模型。

你的情况:

  • Order(我猜的)
  • Customer
  • CustomerName
  • ProductDetails
  • ProductName
  • Phone

然后在 YAML(Swagger 架构文档)的 definitions 部分定义模型:

Order:
  type: "object"
  properties:
    _id:
      type: "string"
    name:
      type: "string"
    descriptions:
      type: "object"
    date:
      type: "string"
    customer:
      $ref: "#/definitions/Customer"
    productDetails:
      $ref: "#/definitions/ProductDetails"
    phoneNumbers:
      type: "array"
      items:
        $ref: "#/definitions/Phone"
Customer:
  type: "object"
  properties:
    id:
      type: "string"
    name:
      $ref: "#/definitions/CustomerName"
CustomerName:       
  type: "object"
  properties:
    firstName:
      type: "string"
    lastName:
      type: "string"
    middleName:
      type: "string"
ProductDetails:
  type: "object"
  properties:
    id:
      type: "string"
    name:
      $ref: "#/definitions/ProductName"
ProductName:       
  type: "object"
  properties:
    name:
      type: "string"
    model:
      type: "string"
    price:
      type: "string"
    comments:
      type: "string"
Phone:
  type: "object"
  properties:
    id: 
      type: "string"
    category:
      type: "string"
    version:
      type: "string"
    condition:
      type: "string"
    availability:
      type: "string"

如果你想定义一个 array 以特定模型作为项目 - 将 array 作为 type 并定义 items (根据提供的错误代码,你忘了它)。 items 是数组的内容 - 因此您的 Phone 模型:

...
phoneNumbers:
  type: "array"
  items:
    $ref: "#/definitions/Phone"