FastApi StaticFiles with mount not working
Describe the bug
Our team is struggling to get fastapi-versioning working in our environment since we are serving static assets for our frontend and docs. This seems to be an issue with mount when using StaticFiles.
To Reproduce Steps to reproduce the behavior:
- Create the file test.py
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi_versioning import VersionedFastAPI, version
app = FastAPI(title="My App")
@app.get("/")
@version(1, 0)
def greet_with_hello():
return "Hello"
@app.get("/")
@version(1, 1)
def greet_with_hi():
return "Hi"
app.mount("/", StaticFiles(directory="/"), name="static")
app = VersionedFastAPI(app)
- Run the server
uvicorn test:app - Error from running application
Traceback (most recent call last):
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/bin/uvicorn", line 8, in <module>
sys.exit(main())
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/main.py", line 362, in main
run(**kwargs)
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/main.py", line 386, in run
server.run()
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/server.py", line 49, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "uvloop/loop.pyx", line 1494, in uvloop.loop.Loop.run_until_complete
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/server.py", line 56, in serve
config.load()
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/config.py", line 308, in load
self.loaded_app = import_from_string(self.app)
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/importer.py", line 20, in import_from_string
module = importlib.import_module(module_str)
File "/usr/local/Cellar/[email protected]/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "./test.py", line 22, in <module>
app = VersionedFastAPI(app)
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/fastapi_versioning/versioning.py", line 42, in VersionedFastAPI
version_routes = [
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/fastapi_versioning/versioning.py", line 43, in <listcomp>
version_to_route(route, default_version) for route in app.routes
File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/fastapi_versioning/versioning.py", line 24, in version_to_route
version = getattr(api_route.endpoint, "_api_version", default_version)
AttributeError: 'Mount' object has no attribute 'endpoint'
Expected behavior A versioned API without having to version mounts.
Additional context N/A
Just got blocked by this same issue.
Getting this same error but not using StaticFiles.
I've solved this issue by using the app.mount after the app = VersionedFastAPI(app)
And if you need to access the root path / (for your landing page for example), define the @app.get and its function after the app = VersionedFastAPI(app) too.
@aminelemaizi thanks for the workaround! I would like to know if this is an acceptable way of doing this or if there needs to be a decorator to exclude mounts for a more official fix?
@killswitch-GUI , tbh I don't know if it is an acceptable way of doing things. I didn't see what does the code under the hood to confirm that.
Following.