0
votes

I am getting errors when i am trying it, but i wanted to create an endpoint which accepts 2 query params and 1 body item, a list of names. When I run it in connexion but says it is an invalid specification.

/devices/list:
  post:
    tags: [Devices]
    operationId: app.get_devices
    summary: Gets a complete list of devices.
    parameters:
      - $ref: '#/parameters/foo-t'
      - $ref: '#/parameters/bar-t'
      - in: body
        name: device_names
        required: true
        type: array
        items:
          type: string
        description: a list of devices
...

It compiles and runs without the - in: body section. So i know the 2 parameters are good. It seems though I am having issue applying sending a json array to python.

Explicitly Returned error is:

connexion.exceptions.InvalidSpecification: {'in': 'body', 'name': 'device_names', 'required': True, 'type': 'array', 'items': {'type': 'string'}, 'description': 'A list of Device Names'} is not valid under any of the given schemas

Failed validating 'oneOf' in schema['properties']['paths']['patternProperties']['^/']['properties']['post']['properties']['parameters']['items']: {'oneOf': [{'$ref': '#/definitions/parameter'}, {'$ref': '#/definitions/jsonReference'}]}

On instance['paths']['/devices/list']['post']['parameters'][2]: {'description': 'A list of Device Names', 'in': 'body', 'items': {'type': 'string'}, 'name': 'device_names', 'required': True, 'type': 'array'}

My desired endstate is that I can say:

//In javascript
$.post("devices/list", {device_names: ["a","b","c"]}, {params:{foo:1, bar:42}}).success( x => {
  //...
});

# In Python3
def get_devices(foo, bar, device_names):
  pass
1

1 Answers

1
votes

Yes, you can mix query and body parameters.

The error is caused by incorrect syntax of the body parameter. Change it so that type and items are wrapped into schema, like this:

      - in: body
        name: device_names
        required: true
        schema:   # <-------
          type: array
          items:
            type: string
        description: a list of devices

In OpenAPI 2.0, non-body parameters (path, query, header, form params) use the type keyword directly, but body parameters must have their type wrapped into schema.


The example above matches a request body that contains a string array as is – ["a", "b", "c"].

If the array is supposed to be wrapped into a JSON object

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

you need to change the body parameter as follows:

      - in: body
        name: device_names
        required: true
        schema:
          type: object
          required: [device_names]
          properties:
            device_names:
              type: array
              items:
                type: string
        description: a list of devices