Sub-app routing no longer depends on middleware ordering

Commit 431d5fb · patx · 2026-01-04T03:43:19-05:00

Changeset
431d5fbb6feff81e9b6151fba088a70804b1313c
Parents
eadd39089cbb0d016ab6774fc0d52f07cfc77f40

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/docs/release_notes.md b/docs/release_notes.md
index 7c8df23..7f6a175 100644
--- a/docs/release_notes.md
+++ b/docs/release_notes.md
@@ -1,6 +1,7 @@
 [![Logo](https://patx.github.io/micropie/logo.png)](https://patx.github.io/micropie)
 
 ## Releases Notes
+- **[0.26](https://github.com/patx/micropie/releases/tag/v0.26)** - Sub-app routing no longer depends on middleware ordering
 - **[0.25](https://github.com/patx/micropie/releases/tag/v0.25)** - Fix unicode redirect handling. Percent-encode non-ASCII path segments before setting Location header. Prevents latin-1 header encoding errors and avoids double-encoding queries.
 - **[0.24](https://github.com/patx/micropie/releases/tag/v0.24)** - Improve session handling. Expired sessions now clean up properly, and empty sessions delete stored data. Session saving also moved after `after_request` middleware.
 - **[0.23](https://github.com/patx/micropie/releases/tag/v0.23)** - Bug fix release. Make sure background multipart parsing stops when the request is terminated by middleware
diff --git a/micropie.py b/micropie.py
index 9d517d5..3b330d0 100644
--- a/micropie.py
+++ b/micropie.py
@@ -19,7 +19,7 @@ import traceback
 import uuid
 from abc import ABC, abstractmethod
 from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple
-from urllib.parse import parse_qs, urlsplit, urlunsplit, quote, quote_plus
+from urllib.parse import parse_qs, urlsplit, urlunsplit, quote
 
 try:
     import orjson as json  # Use `orjson` if installed as it is faster
@@ -547,7 +547,11 @@ class App:
                 await _cancel_parse_task()
                 new_scope = dict(scope)
                 new_scope["path"] = request._subapp_path
-                new_scope["root_path"] = scope.get("root_path", "") + "/" + self.middlewares[0].mount_path
+                mount = getattr(request, "_subapp_mount_path", "").strip("/")
+                if mount:
+                    new_scope["root_path"] = scope.get("root_path", "") + "/" + mount
+                else:
+                    new_scope["root_path"] = scope.get("root_path", "")
                 new_scope["body_params"] = request.body_params
                 new_scope["body_parsed"] = request.body_parsed
                 new_scope["get_json"] = getattr(request, "get_json", {})
diff --git a/pyproject.toml b/pyproject.toml
index 5d75417..cc8443e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"
 
 [project]
 name = "micropie"
-version = "0.25"
+version = "0.26"
 description = "An ultra micro ASGI web framework"
 keywords = ["micropie", "asgi", "microframework", "http"]
 readme = "docs/README.md"