Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define empty array in swagger

Tags:

I know how to make array of string data in swagger like this:

"photoUrls" : {
    "type":"array",
    "items":{
         "type":"string"
    }
}

It will show output like this:

"photoUrls":[
    "string"
]

How to make output like this?:

"photoUrls":[]
like image 429
smftr Avatar asked Apr 14 '15 10:04

smftr


People also ask

How do you specify an array in swagger?

Firstly, we start by specifying the array of strings in Swagger using YAML notation. In the schema section, we include type: array with items String.

What is format in swagger?

OpenAPI Specification (formerly Swagger Specification) is an API description format for REST APIs. An OpenAPI file allows you to describe your entire API, including: Available endpoints ( /users ) and operations on each endpoint ( GET /users , POST /users ) Operation parameters Input and output for each operation.


2 Answers

You can specify an empty array [] as an example for your array schema. This will override the default examples values generated by Swagger UI.

"photoUrls" : {
    "type":"array",
    "items":{
         "type":"string"
    },
    "example": []
}

Ron's answer is more user-friendly for your users. Consider using some real-world example values instead:

"photoUrls" : {
    "type":"array",
    "items":{
         "type":"string",
         "example": "http://example.com/images/pet.png"
    },
}
like image 188
Helen Avatar answered Sep 18 '22 05:09

Helen


You don't. The idea is that

"photoUrls":[
    "string"
]

shows your users that photoUrls is an array of strings. Otherwise, they will have no way of knowing which datatype is used by the array.

like image 25
Ron Avatar answered Sep 18 '22 05:09

Ron