Skip to content

Commit

Permalink
docs: Document static file mount gotcha
Browse files Browse the repository at this point in the history
  • Loading branch information
ofipify authored Mar 7, 2024
1 parent 48a678c commit f92f054
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,11 @@ An APIRouter will be created for each version, with the URL prefix defined by th
- Pass a `callback` param to `Versionizer` and add your own docs/OpenAPI routes manually for each version
- If you want a "main" docs page, with all versioned routes included, you can manually add a docs/OpenAPI route to the versioned FastAPI app returned by `Versionizer.versionize()`.
- See the [Docs Customization](https://github.com/alexschimpf/fastapi-versionizer/tree/main/examples/docs_customization.py) example for more details

## Gotchas

### Static file mounts

If you need to [mount static files](https://fastapi.tiangolo.com/tutorial/static-files/), you'll have to add those to
your FastAPI app **after** instantiating Versionizer. See the [Static file mount](https://github.com/alexschimpf/fastapi-versionizer/tree/main/examples/with_static_file_mount.py)
example for more details.
16 changes: 16 additions & 0 deletions examples/with_static_file_mount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

from fastapi_versionizer import Versionizer

app = FastAPI()

# This will not work!
app.mount('/examples-not-working', StaticFiles(directory='examples'), name='examples')

versions = Versionizer(
app=app,
).versionize()

# Only static file mounts added *after* instantiating Versionizer will work
app.mount('/examples', StaticFiles(directory='examples'), name='examples')
21 changes: 21 additions & 0 deletions tests/test_with_static_file_mount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fastapi.testclient import TestClient

from unittest import TestCase
from examples.with_static_file_mount import app

from pathlib import Path


class TestWithStaticFileMount(TestCase):
def test_with_static_file_mount_example(self) -> None:
test_client = TestClient(app)

# Read example file from file system
expected = Path('examples/with_static_file_mount.py').read_text()

# Compare local file contents with the same retrieved via the static file mount
self.assertEqual(expected, test_client.get('/examples/with_static_file_mount.py').text)

# Check that a static mount before instantiating Versionizer will not work
self.assertEquals('{"detail":"Not Found"}',
test_client.get('/examples-not-working/with_static_file_mount.py').text)

0 comments on commit f92f054

Please sign in to comment.