Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger 2.0: How to specify an input parameter of type 'object'

Using Swagger 2.0 I am trying to specify an input parameter of type object:

code snippet:

paths:
  '/thingies/{thingy_id}.json':
    put:
      summary: Update an existing thingy
      description: Updates an existing thingy
      parameters:
        - name: thingy_id
          description: ID of the thingy to update
          in: path
          required: true
          type: integer
        - name: translation
          description: Name and Locale for new translation
          in: formData
          type: object
          properties:
            name:
              type: string
            locale:
              type: string

However the validator is complaining about the type: object part.

How ought I correctly specify my input parameters?

like image 522
Dave Sag Avatar asked Apr 14 '15 05:04

Dave Sag


People also ask

How do you specify optional parameters in Swagger?

You can use the default key to specify the default value for an optional parameter. The default value is the one that the server uses if the client does not supply the parameter value in the request. The value type must be the same as the parameter's data type.

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

How do you define a parameter in Swagger?

Describing Parameters In Swagger, API operation parameters are defined under the parameters section in the operation definition. Each parameter has name, value type (for primitive value parameters) or schema (for request body), and optional description. Here is an example:

Why does Swagger not allow object inputs?

Swagger allows for object inputs only as body parameters. The reason for that relates to the way content is serialized which depends on the Content-Type header ( produces in Swagger). That header relates to the payload as a whole.

Where are data types used in Swagger?

In the Swagger specification, the data types are used in several locations - Operations, Operation Parameters, Models, and within the data types themselves (arrays). The fields used to describe a given data type are added flatly to the relevant object.

Is it possible to serialize a query parameter in Swagger UI?

OpenAPI 3.0 describes how this is possible here: In the meantime you could just have the query parameter as a plain old string type and then perform the serialization manually, then set the query parameter as required. This is what I'm doing until Swagger UI fully supports OpenAPI 3.0.


1 Answers

Okay, thanks to the input from @ron I have worked out the solution. Yes I need to use body instead of formData but even then it didn't validate, complaining about type: object. But if I define the object first then $ref it then it all works. The following code does validate.

swagger: '2.0'
info:
  version: '1'
  title: Thingy Service
  description: Everyone loves their thingy
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json

definitions:
  localisation:
    type: object
    required:
      - name
      - locale
    properties:
      name:
        type: string
      locale:
        type: string

paths:
  '/thingies/{thingy_id}.json':
    put:
      summary: Update an existing thingy
      description: Updates an existing thingy
      parameters:
        - name: thingy_id
          description: ID of the thingy to update
          in: path
          required: true
          type: integer
        - name: translation
          description: Name and Locale for new translation
          in: body
          schema:
            $ref: '#/definitions/localisation'
      responses:
        204:
          description: No data
        404:
          description: Thingy not found
like image 185
Dave Sag Avatar answered Sep 27 '22 23:09

Dave Sag