Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ASGI root_path for openapi docs #1199

Merged
merged 11 commits into from
Jun 11, 2020

Conversation

iksteen
Copy link
Contributor

@iksteen iksteen commented Mar 31, 2020

ASGI specifies the root_path connection scope variable. This is the path the application is mounted under (the equivalent of WSGI's SCRIPT_NAME).

This PR ensures that this root is used as a prefix for the openapi.json URL, as a prefix for paths contained in openapi.json and by the oauth2 redirect endpoint to redirect back to the swagger docs.

To override the path the ASGI server provides you can still explicitly set the openapi_prefix variable.

Do note that this is a breaking change for applications that overriden openapi() themselves.

This should fix/related to #829

@codecov
Copy link

codecov bot commented Mar 31, 2020

Codecov Report

Merging #1199 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            master     #1199    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          224       230     +6     
  Lines         6728      6862   +134     
==========================================
+ Hits          6728      6862   +134     
Impacted Files Coverage Δ
fastapi/applications.py 100.00% <100.00%> (ø)
tests/test_deprecated_openapi_prefix.py 100.00% <100.00%> (ø)
...t_tutorial/test_behind_a_proxy/test_tutorial001.py 100.00% <100.00%> (ø)
...t_tutorial/test_behind_a_proxy/test_tutorial002.py 100.00% <100.00%> (ø)
fastapi/utils.py 100.00% <0.00%> (ø)
fastapi/routing.py 100.00% <0.00%> (ø)
fastapi/encoders.py 100.00% <0.00%> (ø)
fastapi/openapi/utils.py 100.00% <0.00%> (ø)
tests/test_skip_defaults.py 100.00% <0.00%> (ø)
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6e1cd45...ac517f0. Read the comment docs.

@nunomen
Copy link

nunomen commented Mar 31, 2020

I guess this PR solves the same issue that I was having (I didn't even know about issue #829). I solved my problem by just adding the prefix to the redirect url. You can see what I did on my proposed PR #1159. But it seems to me that your solution involves more steps that I may have been missing. Could you explain?

I will probably delete #1159 since it is redundant, now that you've done this 😄

@tiangolo tiangolo merged commit 2f478ee into tiangolo:master Jun 11, 2020
@tiangolo
Copy link
Owner

tiangolo commented Jun 11, 2020

Great! 🚀

Thanks for the patience, it took me a bit of time to review this as I wanted to first re-read the WSGI and ASGI specs to make sure I wasn't missing anything.

And then to test it all with an actual reverse proxy with path prefix stripping (what the root_path would be used for).

I refactored it a bit and took the opportunity to deprecate openapi_prefix in favor of the new root_path 🔥 🚀

And I added a bunch of docs to explain how the root_path works and to document how to test it all with a real proxy using Traefik.

Thanks for your contribution! 🚀 🎉 🍰

This is available in FastAPI 0.56.0. 🍰

hyahiaoui pushed a commit to hyahiaoui/fastapi that referenced this pull request Aug 1, 2020
* Use ASGI root_path when it is provided and openapi_prefix is empty.

* Strip trailing slashes from root_path.

* Please mypy.

* Fix extending openapi test.

* 📝 Add docs and tutorial for using root_path behind a proxy

* ♻️ Refactor application root_path logic, use root_path, deprecate openapi_prefix

* ✅ Add tests for Behind a Proxy with root_path

* ♻️ Refactor test

* 📝 Update/add docs for Sub-applications and Behind a Proxy

* 📝 Update Extending OpenAPI with openapi_prefix parameter

* ✅ Add test for deprecated openapi_prefix

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants