Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to describe a model in Swagger for an array with simple objects?

I have a REST services to document, some of them accepts simple array like:

[   { "name":"a" },   { "name":"b" },   { "name":"c" } ] 

How do I describe this in Swagger model section ? I can only create 'named array' like

model { properties: { "arr": { "type":"array", ...... 

but it describes data like this:

"arr": [   { "name":"a" },   { "name":"b" },   { "name":"c" } ] 
like image 353
razor Avatar asked Oct 25 '13 09:10

razor


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.

How do you add a description to swagger?

1 - Open the Properties dialog for your project, click the "Build" tab and ensure that "XML documentation file" is checked. This will produce a file containing all XML comments at build-time. At this point, any classes or methods that are NOT annotated with XML comments will trigger a build warning.


1 Answers

Tony YUEN was close, but no cigar. This is the proper definition using YAML in OpenAPI/Swagger:

  /test: post:   summary: test 123   description: test 123   parameters:     - name: param1       in: body       required: true       description: test param1       schema:           $ref: '#/definitions/stackoverflow'   responses:     200:       description: OK 

This produces:

stackoverflow2[   {      name: string   } ] 

Tony's example produces:

[   stackoverflow {                   name: string   } ] 

Complete Swagger/OpenAPI as YAML (copy & paste)

    swagger: '2.0'  ################################################################################ #                              API Information                                 # ################################################################################ info:   version: "Two-point-Oh!"   title: Simple objects in array test   description: |     Simple objects in array test  ################################################################################ #                                   Parameters                                 # ################################################################################  paths:   /test:     post:       summary: Array with named objects       description: Array with named objects       parameters:         - name: param1           in: body           required: true           description: test param1           schema:             type: array             items:               $ref: '#/definitions/stackoverflow'       responses:         200:           description: OK   /test2:     post:       summary: Array with simpel (nameless) objects       description: Array with simpel (nameless)  objects       parameters:         - name: param1           in: body           required: true           description: test param1           schema:               $ref: '#/definitions/stackoverflow2'       responses:         200:           description: OK definitions:   stackoverflow:     type: object     properties:       name:         type: string         description: name of the object   stackoverflow2:     type: array     items:       type: object       properties:         name:           type: string           description: name of the object 

Here's a JSON-version of Swagger/OpenAPI

    {   "swagger" : "2.0",   "info" : {     "description" : "Simple objects in array test\n",     "version" : "Two-point-Oh!",     "title" : "Simple objects in array test"   },   "paths" : {     "/test" : {       "post" : {         "summary" : "Array with named objects",         "description" : "Array with named objects",         "parameters" : [ {           "in" : "body",           "name" : "param1",           "description" : "test param1",           "required" : true,           "schema" : {             "type" : "array",             "items" : {               "$ref" : "#/definitions/stackoverflow"             }           }         } ],         "responses" : {           "200" : {             "description" : "OK"           }         }       }     },     "/test2" : {       "post" : {         "summary" : "Array with simpel (nameless) objects",         "description" : "Array with simpel (nameless)  objects",         "parameters" : [ {           "in" : "body",           "name" : "param1",           "description" : "test param1",           "required" : true,           "schema" : {             "$ref" : "#/definitions/stackoverflow2"           }         } ],         "responses" : {           "200" : {             "description" : "OK"           }         }       }     }   },   "definitions" : {     "stackoverflow" : {       "type" : "object",       "properties" : {         "name" : {           "type" : "string",           "description" : "name of the object"         }       }     },     "stackoverflow2" : {       "type" : "array",       "items" : {         "$ref" : "#/definitions/stackoverflow2_inner"       }     },     "stackoverflow2_inner" : {       "properties" : {         "name" : {           "type" : "string",           "description" : "name of the object"         }       }     }   } } 
like image 52
Asgair Avatar answered Sep 20 '22 04:09

Asgair