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

Swagger UI assumes body payload when using api_ns.expect(model) for HTTP GET handler

Open mr-tabasco opened this issue 7 years ago • 5 comments
trafficstars

Looking through the the docs, there's an example of setting a model as an expected input to the get request handler, and to me it would be reasonable to assume that restplus would use this model to validate query string parameters as it's the only place that would make sense for it to be in the request. When using the same model for a post, swagger UI renders it as body request parameter, which makes sense. I'm just wondering if I'm wrong in my assumptions and this is by design, or it's a bug?

Current version of flask-restplus: 0.11.0

class SomeResource(Resource):
    
    @my_api_ns.expect(my_super_cool_model)
    def get(self):
        # This will render as a body request param, not expected
        return {}

    @my_api_ns.expect(my_super_cool_model)
    def post(self):
        # This will render as a body request param, as expected
        return {}

mr-tabasco avatar Jun 04 '18 13:06 mr-tabasco

I met the same issue.

shenfe avatar Jun 06 '18 06:06 shenfe

Use parser to solve this.

parser = api.parser()
parser.add_argument('param', type=int, help='Some param', location='path') # `location` specifies the parameter type

@api.expect(parser)
...

shenfe avatar Jun 06 '18 07:06 shenfe

I've encountered the same issue. I've worked around it for now with parser, but it seems like you lose some of the flexibility of models going that way. Also, parser appears to be a deprecated piece of functionality.

Is there a model based way to achieve this? Digging around in the source it all gets passed through the doc method, so I'm guessing there must be some way to do it, I just haven't figured it out yet.

martsa1 avatar Jun 06 '18 09:06 martsa1

You could put it into the doc yourself with the doc method, but there isn't currently a model-based way of achieving this.

martijnarts avatar Jun 08 '18 12:06 martijnarts

Any update on this issue? I am facing the same problem, wanting to use model as my @api.expect rather than parser due to the limited flexibility of parser.

delewis13 avatar Dec 23 '19 04:12 delewis13