Repository: alexschimpf/fastapi-versionizer (language: python)

Issue:
Mounting static files requires Versionizer to be instantiated before adding a mount
### Subject of the issue

When mounting static files, the order of operations is important.

### Your environment
* Python 3.11.3
* Manjaro Linux

### 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.

```python
# 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.

```python
# 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.


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

Fix patch:
diff --git a/README.md b/README.md
index 08ffa62..df45d94 100644
--- a/README.md
+++ b/README.md
@@ -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.
\ No newline at end of file
diff --git a/examples/with_static_file_mount.py b/examples/with_static_file_mount.py
new file mode 100644
index 0000000..9732255
--- /dev/null
+++ b/examples/with_static_file_mount.py
@@ -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')


Test patch:
diff --git a/tests/test_with_static_file_mount.py b/tests/test_with_static_file_mount.py
new file mode 100644
index 0000000..859f0fa
--- /dev/null
+++ b/tests/test_with_static_file_mount.py
@@ -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)