I would like to post an array of strings like
[
"id1",
"id2"
]
to a Swagger based API. In my swagger file, I have those lines:
paths:
/some_url:
post:
parameters:
- name: ids
in: body
required: true
What is the correct way to specify the type of ids
as an array of strings?
Update:
According to the specification, the following should work in my option:
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 convoluted errors, which look like the code expects some $ref
.
If you are trying to send a body with multiple parameters, add an object model in the definitions section and refer it in your body parameter, see below (works with editor.swagger.io):
In Swagger terms, the request body is called a body parameter. There can be only one body parameter, although the operation may have other parameters (path, query, header). Note: The payload of the application/x-www-form-urlencoded and multipart/form-data requests is described by using form parameters , not body parameters.
Let's see how to add one. 3. YAML 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. To better document the API and instruct the user, we can use the example label of how to insert values:
1. Overview Swagger is a set of specifications to document and describe REST APIs. It also provides example values for the endpoint parameters. In this tutorial, we'll show how to produce a default example value for String arrays, as this behavior is not enabled by default. 2.
In such case, your swagger document need to be modified as follows: When request body is json, a parameter with in: body is used instead of multiple parameters of in: formData. If in is body, a schema object is required. Defined the json properties under schema.
Your description of an array of string is correct, but the parameter definition misses the name
property to be valid.
Here's a full 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 your OpenAPI (fka. Swagger) specs: http://editor.swagger.io/
I have created a swagger issue as the help provided by Arnaud, although is valid yaml, will give you NPE exceptions when trying to generate. You will need to provide an object like 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 item etc):
schema:
$ref: "#/definitions/myDataItem"
For reference the github issue:
https://github.com/swagger-api/swagger-codegen/issues/6745
Note, the issue has been fixed in version 2.3.0 and higher, ideally you should upgrade to that version.
None of the answers worked for me. As it is stated in the following Baeldung article:
To better document the API and instruct the user, we can use the example label of how to insert values
So the full working example would be something like that:
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
example: ["str1", "str2", "str3"]
responses:
default:
description: OK
You can check how the Example Value is now better informed in the Swagger editor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With