Specify an array of strings as the body parameter in the swagger API.

I would like to post an array of strings like

[ "id1", "id2" ] 

for the Swagger based API. In my swagger file, I have these lines:

 paths: /some_url: post: parameters: - name: ids in: body required: true 

What is the correct way to specify the ids type as an array of strings?

Update:

According to the specification in my version, the following should work:

  parameters: - in: body description: xxx required: true schema: type: array items: type: string 

https://github.com/Yelp/swagger_spec_validator does not accept it and returns a long list of confusing errors that look as if the code expects a few $ref .

+8
rest swagger
source share
3 answers

Your string array description is correct, but the parameter definition skips the name property.

Here is a complete working example:

 swagger: "2.0" info: title: A dummy title version: 1.0.0 paths: /path: post: parameters: - in: body description: xxx required: true name: a name schema: type: array items: type: string responses: default: description: OK 

Try the online editor to check the OpenAPI specifications (fka. Swagger): http://editor.swagger.io/

+15
source share

For an array containing an object as content, an object definition can also be expressed using definitions and $ ref . Example:

schema: type: array items: $ref: '#/definitions/ObjectSchemaDefinition' definitions: ObjectSchemaDefinition: type: string

+2
source share

I created a problem on a grand scale, as the help provided by Arno, although it is a valid yaml, will give you NPE exceptions when trying to generate. You will need to provide an object similar to the following:

  myDataItem: type: object description: A list of values required: - values properties: values: type: array items: type: string 

And then refer to it (in your post, etc.):

  schema: $ref: "#/definitions/myDataItem" 

For reference: problem with github:

https://github.com/swagger-api/swagger-codegen/issues/6745

Note. The problem has been fixed in version 2.3.0 and higher, ideally you should upgrade to this version.

+1
source share

All Articles