noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 505 forks source link

Add expected headers AND model #511

Open pistacchio opened 6 years ago

pistacchio commented 6 years ago

Hi, I'm using @api.expect(MyModel, validate=True) in order to validate the input json of a post request again a custom model. I've created the model with MyModel = api.model('MyModel', {...}).

I also want to add a required (expected) header to be handled and validated by the request. I can do this:

parser = api.parser()
parser.add_argument('X-Authentication', location='headers')
[...]
@api.expect(parser)

But how can I expect and validate a model and a header since api.parser and add_argument lets me add headers but doesn't seem to handle a "add this model as body payload" while api.model doesn't let me add headers?

Thanks

jhallard commented 5 years ago

Did you ever figure this out? Having the exact problem.

thierryNano commented 5 years ago

Also looking for a solution to this issue

Cuahchic commented 5 years ago

I am also looking to do the same thing. It's a real shame because I really do like Flask but the Swagger integration is a bit all over the place.

Reed-Bigelow commented 5 years ago

If anyone in the future is looking to set both the body and parser inside the doc, this will add both to the Swagger documentation. parser = api.parser() parser.add_argument('API_KEY', type=str, location='headers') @api.doc('Example description.', parser=parser, body=example_model)

tmsteem82dev commented 4 years ago

You can also do it by passing both the parser and the model as parameters to @api.expect:

my_part_schema_model = {
 "part_name": "test_name",
 "part_type": "test_type"
}
parser = api.parser()
parser.add_argument("Auth", location = "headers", help = "authentication token")
model = api.schema_model( "mymodel", my_part_schema_model)
...

@api.expect(parser, model, validate=True)
...