Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger array of integer return type

I'm just getting started with using the swagger-editor to define my RESTful API and I'm confused on the responses. Many of my methods simply return an array of integers, and I'm not understanding how to specify that in the YAML.

like image 388
Gargoyle Avatar asked Jan 19 '17 04:01

Gargoyle


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 does swagger define byte array?

Using the definition above the swagger code generator generates an object that accepts byte[] array as the body field new Job(). setBody(new byte[1]) . After converting the API definition to OpenAPI the definition for that object stayed the same but the openapi code generator now requires org. springframework.

How do you pass multiple parameters in swagger?

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):


Video Answer


1 Answers

The OpenAPI (fka Swagger) Specification 2.0 uses a subset of JSON Schema v4. You can refer to the JSON Schema docs or this great guide to learn how to describe different data types using JSON Schema. But keep in mind that some features of JSON Schema are not supported or work differently in OpenAPI/Swagger. The Specification mentions what exactly is supported.

Back to your question, an array of integers is defined as:

type: array
items:
  type: integer

Or in the context of a response:

paths:
  /something:
    get:
      responses:
        200:
          description: OK
          schema:
            type: array
            items:
              type: integer

If arrays of integers are used in multiple places in your spec, you can define the array in the global definitions section and then use $ref to refer to it:

paths:
  /something:
    get:
      responses:
        200:
          description: OK
          schema:
            $ref: "#/definitions/ArrayOfInt"

definitions:
  ArrayOfInt:
    type: array
    items:
      type: integer

You can also specify example values for the array. Swagger UI will display this example, and some mocking tools will use it when generating sample responses.

definitions:
  ArrayOfInt:
    type: array
    items:
      type: integer
    example: [1, 2, 3, 4]
    # Make sure to put the multi-item "example"
    # on the same level as the "type" and "items" keywords
like image 143
Helen Avatar answered Nov 17 '22 09:11

Helen