Как создать массив Swagger - PullRequest
0 голосов
/ 20 ноября 2018

Я пытаюсь создать документ подкачки для 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 голосов
/ 21 ноября 2018

Прежде всего вы должны определить модели, зависящие от 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"

Если вы хотите определить массив с конкретной моделью в качестве элемента - возьмите arrayкак type и определите items (согласно предоставленному коду ошибки вы его забыли).items - это содержимое массива, поэтому модель Phone в вашем случае:

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