简体   繁体   English

如何创建swagger数组

[英]How to create swagger array

I'm trying to create a swagger doc for below JSON, but I'm getting the below error: schemas with 'type: array', require a sibling 'items: ' field 我正在尝试创建一个低于JSON的swagger文档,但我得到以下错误: 带有'type:array'的模式,需要兄弟'items:'字段

JSON: 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"

        }
    ]   
}

Can someone help me to get the swagger doc for this JSON. 有人可以帮助我获得这个JSON的swagger文档。

Any help would be really appreciated. 任何帮助将非常感激。

First of all you have to define models dependent on the JSON (objects). 首先,您必须定义依赖于JSON(对象)的模型。

In your case: 在你的情况下:

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

Then define the models in definitions section of the YAML (Swagger schema documentation): 然后在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"

If you want to define an array with a specific model as item - take array as a type and define items (according to provided error code you forgot it). 如果要将具有特定模型的数组定义为项目 - 将array作为type并定义items (根据提供的错误代码,您忘记了它)。 items is the content of the array - so the the Phone model in your case: items是数组的内容 - 所以在你的情况下的Phone模型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM