learn groovy
learn groovy

Reputation: 527

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:

{
    "_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.

Any help would be really appreciated.

Upvotes: 9

Views: 17329

Answers (1)

Vladas Maier
Vladas Maier

Reputation: 2152

First of all you have to define models dependent on the JSON (objects).

In your case:

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

Then define the models in definitions section of the YAML (Swagger schema documentation):

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). items is the content of the array - so the the Phone model in your case:

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

Upvotes: 13

Related Questions