【问题标题】:How to create swagger array如何创建招摇阵列
【发布时间】:2018-11-20 17:59:06
【问题描述】:

我正在尝试为以下 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"

        }
    ]   
}

谁能帮我获取这个 JSON 的 swagger 文档。

任何帮助将不胜感激。

【问题讨论】:

    标签: json swagger swagger-2.0


    【解决方案1】:

    首先你必须定义依赖于 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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-23
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      相关资源
      最近更新 更多