如何创建摇摇欲坠的数组

时间:2018-11-20 17:59:07

标签: json swagger swagger-2.0

我正在尝试为低于JSON的文档创建一个草率的文档,但出现以下错误:具有'type:array'的方案,需要同级的'items:'field

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的详尽信息吗?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

首先,您必须定义依赖于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作为type并定义items(根据提供的错误代码您忘记了)。 items是数组的内容-因此,Phone模型在您的情况下:

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