如何创建swagger数组

lea*_*ovy 6 json swagger swagger-2.0

我正在尝试为以下 JSON 创建一个 swagger 文档,但我收到以下错误:带有“类型:数组”的模式,需要一个兄弟“项目:”字段

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"

        }
    ]   
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我获取此 JSON 的 swagger 文档吗?

任何帮助将非常感激。

Vla*_*ier 8

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

在你的情况下:

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

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

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

如果您想将具有特定模型的数组定义为 item - 将其array作为 atype并定义items(根据您忘记的提供的错误代码)。items是数组的内容 - 所以Phone你的例子中的模型:

...
phoneNumbers:
  type: "array"
  items:
    $ref: "#/definitions/Phone"
Run Code Online (Sandbox Code Playgroud)