Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

base href for swagger spec and ui #544

Closed
eterna2 opened this issue Sep 17, 2019 · 8 comments
Closed

base href for swagger spec and ui #544

eterna2 opened this issue Sep 17, 2019 · 8 comments

Comments

@eterna2
Copy link

eterna2 commented Sep 17, 2019

Is your feature request related to a problem? Please describe.
Our fastapi service is place behind a proxy using path-based routing - i.e. /docs becames /foo/docs

This result in the default swagger spec as well as the swagger UI to fails as the openapi path passed to the html is an absolute ref (i.e. /docs or /prefix/docs) which will resolve incorrectly if there is a non-fastapi path routing.

See

openapi_url = self.openapi_prefix + self.openapi_url

Describe the solution you'd like
Either incorporate a base href argument to fast API, or set the openapi_url to relative ref (i.e. ./docs or ./prefix/docs).

Similar fix should be done for the openapi spec generation.

I would prefer the former solution as it will also correctly generate the openapi spec (to include the non-fastapi prefix).

Describe alternatives you've considered
Currently I provision my own custom route with the modified swagger UI HTML as well as the openapi spec.

@eterna2 eterna2 added the feature New feature or request label Sep 17, 2019
@eterna2
Copy link
Author

eterna2 commented Sep 17, 2019

I can probably do a PR if it can expedite this feature request.

@euri10
Copy link
Contributor

euri10 commented Sep 17, 2019 via email

@eterna2
Copy link
Author

eterna2 commented Sep 17, 2019

Ah. I didn't realise there is this option.

Thanks. Problem solved.

@eterna2 eterna2 closed this as completed Sep 17, 2019
@eterna2 eterna2 reopened this Sep 17, 2019
@eterna2
Copy link
Author

eterna2 commented Sep 17, 2019

Reopening this issue for more clarity.

My service is deployed in kubernetes and API gateway is serviced with ambassador. The path prefix is serviced by ambassador not by uvicorn.

I am routing ambassador /foo/api to /api in uvicorn which will result in this issue. But I probably can workaround this by setting the route the same both in ambassador and in uvicorn

i.e. route ambassador /foo/api to /foo/api in uvicorn.

Let me try tomorrow and I will close this issue if it works.

@tiangolo
Copy link
Owner

Thanks for the help here @euri10 ! 👏 🙇

Thanks for reporting back and closing the issue @eterna2 👍

@tiangolo
Copy link
Owner

tiangolo commented Jun 11, 2020

Just for completeness, #1199 added support for root_path, and there are new docs for running behind a proxy here: https://fastapi.tiangolo.com/advanced/behind-a-proxy/

It's available in FastAPI 0.56.0.

@MatthewScholefield
Copy link

To summarize, now you can just pass root_path='/api/v1' to FastAPI(...). One thing to note, I realized I was having issues with authorization because I was using tokenUrl="/token" instead of tokenUrl="token" in OAuth2PasswordBearer(...).

@tiangolo tiangolo added question Question or problem answered reviewed and removed feature New feature or request labels Feb 22, 2023
@tiangolo tiangolo changed the title [FEATURE] base href for swagger spec and ui Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8082 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

4 participants