flask-restplus icon indicating copy to clipboard operation
flask-restplus copied to clipboard

Fixing Swagger Issue when using @api.expect() on a request parser

Open tnweiss opened this issue 6 years ago • 2 comments

Below is a request parser for a simple hello world server.

parser = reqparse.RequestParser()
parser.add_argument('test', type=int, location='json')
parser.add_argument('test1', location='json')

The resulting swagger JSON for these parameters is

"parameters": [
    {
         "name": "test",
          "in": "body",
          "type": "integer"
     },
     {
          "name": "test1",
           "in": "body",
           "type": "string"
       }
]

The result should be something similar to when a user provides a model with api.expect()

"parameters": [
    {
        "name": "payload",
        "required": true,
        "in": "body",
        "schema": {
            "properties": {
                "test": {
                    "type": "integer"
                },
                "test1": {
                    "type": "string"
                }
            }
        }
    }
]

These changes fix that bug.

tnweiss avatar Sep 30 '19 00:09 tnweiss

Coverage Status

Coverage decreased (-0.009%) to 96.896% when pulling 1ad99695e23eca46c3e660aa06258d45d65f1a35 on tnweiss:master into a7e363a8352efc70c8d160ef9526dc4572733a1e on noirbizarre:master.

coveralls avatar Sep 30 '19 00:09 coveralls

Great job, Thanks While it's not avaliable at 2020/11, I just use your commit as a monkey patch so solve my problem. Thanks aloooooooooooooooot!

By the way, I found that 'type': 'object' should be inside of schema for version 0.13.0

def rparser_to_swagger_body_param(request_parser):
    """
    If any parameters are in the request body then return the swagger representation for the requests json body
    :param request_parser: The request parser containing params for the request
    :return:
    """
    json_body_list = [p for p in request_parser.__schema__ if p['in'] == 'body']
    if not json_body_list:
        return

    properties = {}
    for param in json_body_list:
        properties[param['name']] = {
            'type': 'string' if 'type' not in param else param['type']
        }

    return {
        'name': 'payload',
        'required': True,
        'in': 'body',
        'schema': {
            'type': 'object',
            'properties': properties
        }
    }

wood-j avatar Nov 11 '20 02:11 wood-j