python-restx / flask-restx

Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
https://flask-restx.readthedocs.io/en/latest/
Other
2.16k stars 335 forks source link

How to generate postman config working example #437

Open boompig opened 2 years ago

boompig commented 2 years ago

Hi all,

I'm having trouble exporting postman configs for even basic flask-restx apps. As an example, if we use the TodoDAO app given on the documentation page for a full example, then use code similar to that given on the Postman documentation page, this error comes up:

Traceback (most recent call last): File "export_postman.py", line 7, in data = api.as_postman(urlvars=urlvars, swagger=swagger) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/api.py", line 803, in as_postman return PostmanCollectionV1(self, swagger=swagger).as_dict(urlvars=urlvars) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/postman.py", line 199, in as_dict "id": self.id, File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/postman.py", line 157, in id return str(self.uuid) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/postman.py", line 153, in uuid return uuid5(NAMESPACE_URL, self.api.base_url) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/api.py", line 551, in base_url return url_for(self.endpoint("root"), _scheme=self.url_scheme, _external=True) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask/helpers.py", line 272, in url_for raise RuntimeError( RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.

This is mentioned in another GitHub issue. The fix suggested there is to run in the context of the application with a context manager:

with app.app_context(): see_json()

def see_json(): urlvars = False # Build query strings in URLs swagger = True # Export Swagger specifications data = api.as_postman(urlvars=urlvars, swagger=swagger) f = open('postman_import.json', 'w') f.write(json.dumps(data))

However this produces yet another error for me:

Traceback (most recent call last): File "export_postman.py", line 14, in see_json() File "export_postman.py", line 9, in see_json data = api.as_postman(urlvars=urlvars, swagger=swagger) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/api.py", line 803, in as_postman return PostmanCollectionV1(self, swagger=swagger).as_dict(urlvars=urlvars) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/postman.py", line 199, in as_dict "id": self.id, File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/postman.py", line 157, in id return str(self.uuid) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/postman.py", line 153, in uuid return uuid5(NAMESPACE_URL, self.api.base_url) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask_restx/api.py", line 551, in base_url return url_for(self.endpoint("root"), _scheme=self.url_scheme, _external=True) File "/Users/my_user/prog/todo-mvc/env/lib/python3.8/site-packages/flask/helpers.py", line 298, in url_for raise RuntimeError( RuntimeError: Application was not able to create a URL adapter for request independent URL generation. You might be able to fix this by setting the SERVER_NAME config variable.

In any case, it seems like the documentation to generate postman config on the documentation page is perhaps out of date, or maybe missing steps. I would appreciate any clarification if possible.

boompig commented 2 years ago

Flask version: 2.1.2 Flask-restx version: 0.5.1