-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intuit request parameters from reqparse? #18
Comments
Hi @DeaconDesperado thanks for the suggestion! |
I'll brew together an example and post here a little later 馃槃 |
+1 this seems like the way to go, did you get anywhere with it Deacon? If not do you have any pointers for things to ensure / avoid? Any help is appreciated. Cheers =) |
@chrisfranklin Sorry for the late reply on this. I did have a crack at it, but nothing too substantial came of it because halfway through implementation I had to seriously rethink my intended design. From what I can tell, Swagger relies pretty heavily on the notion of a "response class", which is basically a complete data object. The original angle I was taking this from was just to handle the request parsing part, but generating to this class footprint is a little more complicated. I'll have a look back at it and reply again here. If I've perhaps misunderstood the internals of swagger, I encourage others to chime in. I think it would be really cool to have the option to sit the doc generation atop a convention already existing in flask-restful. 馃槃 |
Thanks for the response, all useful information, work aren't going to sign off on this until I'm sure it won't just be a time drain but I'll have a look at it over the weekend see if I can chime in at all. You are absolutely right though it would be really cool to have it use reqparse. I've used the Django Restframework Swagger integration before and it worked extremely well. It might be worth a look for seeing how they got round the issue? https://github.com/marcgibbons/django-rest-swagger |
+1 for reqparse |
@DeaconDesperado any updates on this? |
+1 on this too. The way I seeing it being used intropectively is to use the @swagger.operation decorator, but omit the parameters arg. The decorator would then look at the method being decorated for any RequestParser() objects. If it finds one, it them looks at all the arguments (created using What would be missing is the paramType and allowMultiple dictionary entries. The decorator could take either defaults (ie paramType to be used on all parameters) or else take a parameters list with partial dictionaries which will map (as applicable) to the discovered reqparse entries (ie Even though it splits up the parameters fields into 2 sections of code (explicit and reqparse fields), it prevents duplicated data which may diverge. |
I went ahead and implemented the above by adding these lines to swagger.py beginning at line 181 (see pull request #39):
I'll do a pull request or whatever (I dont use git much) so it can be rolled into the next rev. To be sure context is clear, here's what I added again but with a few lines before and after:
There's a catch though: one must be particular in where they define their ReqParse objects in their class: For this patch to be useful, define your "parser" (same object names/types as flask-restful docs) within your Resource-based class, but outside your methods. Lastly, you must then add a line after your method to pin the parser you defined to the method itself. This is because while functions can have attributes, methods cannot, unless the attribute is added to the method from within its class. For example:
What you should experience is that swagger adds data from your ReqParser into its parameters, thereby letting you have just one "master" in where your parameter definitions live. Otherwise, you'd have to update both your ReqParser and swagger.operation decorator whenever you made a change. Hope this helps! |
Per issue rantav#18, added code to look for a "parser" attribute on a method decorated by @swagger.operation. If parser is found, then it assumes it to be a ReqParser object and extracts overlapping data between ReqParser and Swagger: name, required, description/help and dataType/type. This requires ReqParser to be defined and added to method as an attribute per this issue comment: rantav#18 (comment)
I would love to see this too, makes perfect sense.
add another elif onto swagger.add_model
use it:
Edit: no need to declare the parser outside of MyPostInput |
I ended up doing something different to the above that gives more flexibility with inputs of different paramType/location. I just added a "reqparser" arg on swagger.operation(). Scoping is a little smelly, but not too bad, imo. usage: class UserResource(Resource):
getUserParser = reqparse.RequestParser()
getUserParser.add_argument('flags', type=bool, location='args')
getUserParser.add_argument('contact', type=bool, location='args')
@swagger.operation(
notes='Get user details',
nickname='getUser',
reqparser=getUserParser
)
def get(self, username):
args = self.getUserParser.parse_args()
return get_user(username, **args), 200
# if parser.arg.location has "json" (default), then a model is
# created and used on the swagger output
updateUserParser = reqparse.RequestParser()
updateUserParser.add_argument('address', type=str, required=True,
help="You must supply an address")
@swagger.operation(
notes='Update user details',
nickname='updateUser',
reqparser=updateUserParser,
# reqparser args and parameters get merged
parameters=[
{"name": "some_other_param",
"paramType": "query",
"dataType": "string"
}
]
)
def put(self, username):
args = self.updateUserParser.parse_args()
if args.some_other_param:
do_something()
return update_user(username, args.address), 204 |
Hi djm I think the issue with the implementation below is that For this reason, i chose to have a dictionary which would drive creating a I'll aim to get you some code soon! Jason On Tue, Aug 26, 2014 at 3:18 PM, djm notifications@github.com wrote:
|
Hi Jason, See the commit I linked to above for how to iterate through reqparse args (line 326). Also, yes, some translation (quite a bit actually) of reqparse args is needed. e.g. action=='append' maps to allowMultiple (line 374). A little bit off-topic, but: an annoyance that needs addressing: reqparse arg type="bool" is useless as a swagger "boolean" without some hackery. A query string sent by swagger-ui as a "boolean" such as: flags=false or flags=true are both True for python :( cheers |
Great, I will take a look at the commit! How important is it to you that reqparse and swagger are tethered by I think the reason the bools always shows true for python is because it Lastly, on the autogenerated API web page, i find that the GET requests Jason On Wednesday, August 27, 2014, djm notifications@github.com wrote:
|
Hi There, I looked at your code you linked to me Regarding your bool problem, i did some digging and i think row 369 needs Unlike parsing resource_fields, it seems that reqparse doesn't really store
This may fix your bool problem, and likely other problems you might run Jason On Wed, Aug 27, 2014 at 3:38 PM, Jason Haury jason.haury@gmail.com wrote:
|
Nah, the type is being recognised as a bool - that's not the problem. FYI, issubclass() is being called, not isinstance() - which is why I moved the test for bool up above the test for int (bool is a subclass of int, so 'integer' was being returned, short-circuiting the test for bool). BTW, yes, post requests work fine for me (as do delete and put). |
I've now begun using bool types and see more of what you mean. Is it The reason POSTs weren't working was because I hadn't set the BTW, I noticed the flask-restplus Jason On Thu, Aug 28, 2014 at 5:17 PM, djm notifications@github.com wrote:
|
I didn't know about flask-restplus, looks kind of new. I wonder if that dude know about flask-restful-swagger. I'd be happy to cooperate, does someone know the guys (his email is hidden on GH so I though I might take a more personal route before opening an issue to get his attention ;)) |
Hah, i don't know who he is. I do know that his package requires python Is there anything I can help with for this reqparse? I see that the pull Jason On Thu, Sep 11, 2014 at 1:08 AM, Ran Tavory notifications@github.com
|
What's the status on this? Is this going to pulled or not? I am very interested in this. |
First off, great work! 馃槃
An idea: Flask-restful has a module called
reqparse
that is intended to be used as the main way to consume request parameters from within an endpoint. I am wondering if it could be useful to introspect on an endpoint'sreqparse.RequestParser
instance to generate the params for the swagger docs? I think it could be really cool to have one interface for generating this as a high-level option, where the@swagger.operation
decorator could still be very useful for more granular specs.What do you think? I could see some challenges implementing this (mainly where to scope the
RequestParser
instance). I'd be willing to have a crack at it if you think it's a compelling case.The text was updated successfully, but these errors were encountered: