alexschimpf / fastapi-versionizer

FastAPI Versionizer
MIT License
79 stars 13 forks source link

Mounting static files requires Versionizer to be instantiated before adding a mount #55

Closed ofipify closed 6 months ago

ofipify commented 6 months ago

Subject of the issue

When mounting static files, the order of operations is important.

Your environment

Steps to reproduce

Putting a file in images and then trying to open it, e.g. http://127.0.0.1:8000/images/foo.png does not work.

# Mount static files so that we can include screenshots in the documentation
app.mount("/images", StaticFiles(directory="images"), name="images")

# Setup API versioning
versions = Versionizer(
    app=app,
    prefix_format='/v{major}',
    semantic_version_format='{major}',
    default_version=(1, 0),
    sort_routes=True
).versionize()

Expected behaviour

Static file mounts should be accessible.

Actual behaviour

Static files won't be accessible and one gets {"detail":"Not Found"}

Workaround

Calling app.mount after instantiating Versionizer works.

# Setup API versioning
versions = Versionizer(
    app=app,
    prefix_format='/v{major}',
    semantic_version_format='{major}',
    default_version=(1, 0),
    sort_routes=True
).versionize()

# Mount static files so that we can include screenshots in the documentation
# This *has* to be called after Versionizer otherwise it will not work
app.mount("/images", StaticFiles(directory="images"), name="images")

I'm not entirely sure this is actually an issue, but maybe something that should be documented (happy to create a PR). If nothing else, this issue might help somebody coming across the same problem like I had.

@alexschimpf thank you for this great library.

alexschimpf commented 6 months ago

Yeah, you are absolutely right. A PR to update documentation would be greatly appreciated! :)

alexschimpf commented 6 months ago

PR has been merged. PyPI description will get updated during the next deployment.