fix:routen2
This commit is contained in:
1 parent
90fbbffc20
commit
f18298d07f
2 files changed
+24
-8
No files matched your search
@@ -34,12 +34,22 @@ PROTECTED_PREFIXES = ("/admin", "/forms", "/account")
|
||||
DUMMY_ID = "00000000-0000-0000-0000-000000000000"
|
||||
|
||||
|
||||
def _get(obj: Any, name: str) -> Any:
|
||||
"""Attribut oder Schlüssel (Einbindungs-Kontext kann beides sein)."""
|
||||
if isinstance(obj, dict):
|
||||
return obj.get(name)
|
||||
return getattr(obj, name, None)
|
||||
|
||||
|
||||
def _dep_calls(obj: Any) -> set[Any]:
|
||||
"""Abhängigkeiten eines Endpunkts oder Routers (Depends(...) und aufgelöster Baum)."""
|
||||
"""Abhängigkeiten eines Endpunkts oder Routers (Depends(...) und aufgelöster Baum),
|
||||
auch aus dem Einbindungs-Kontext (FastAPI ab 0.16x: include_router(dependencies=...))."""
|
||||
calls: set[Any] = set()
|
||||
for dep in getattr(obj, "dependencies", None) or ():
|
||||
if getattr(dep, "dependency", None) is not None:
|
||||
calls.add(dep.dependency)
|
||||
for holder in (obj, _get(obj, "include_context")):
|
||||
for dep in _get(holder, "dependencies") or () if holder is not None else ():
|
||||
for name in ("dependency", "call"):
|
||||
if getattr(dep, name, None) is not None:
|
||||
calls.add(getattr(dep, name))
|
||||
todo = [getattr(obj, "dependant", None)]
|
||||
while todo:
|
||||
node = todo.pop()
|
||||
@@ -56,6 +66,10 @@ def _is_endpoint(obj: Any) -> bool:
|
||||
return bool(getattr(obj, "methods", None)) and isinstance(getattr(obj, "path", None), str)
|
||||
|
||||
|
||||
def _is_routing(obj: Any) -> bool:
|
||||
return any(hasattr(obj, name) for name in ("path", "routes", "original_router"))
|
||||
|
||||
|
||||
def _children(obj: Any) -> list[Any]:
|
||||
"""Untergeordnete Routen/Router, ohne sich auf eine bestimmte FastAPI-Version zu verlassen."""
|
||||
found: list[Any] = []
|
||||
@@ -67,8 +81,8 @@ def _children(obj: Any) -> list[Any]:
|
||||
"original_router"))
|
||||
for value in values:
|
||||
if isinstance(value, (list, tuple)):
|
||||
found.extend(v for v in value if hasattr(v, "path") or hasattr(v, "routes"))
|
||||
elif value is not None and (hasattr(value, "routes") or _is_endpoint(value)):
|
||||
found.extend(v for v in value if _is_routing(v))
|
||||
elif value is not None and (_is_routing(value) and not isinstance(value, str)):
|
||||
found.append(value)
|
||||
return found
|
||||
|
||||
@@ -83,7 +97,8 @@ def _routes(app: Any) -> list[tuple[str, Any, set[Any]]]:
|
||||
if depth > 8 or id(obj) in seen:
|
||||
return
|
||||
seen.add(id(obj))
|
||||
own = getattr(obj, "prefix", None) or ""
|
||||
own = getattr(obj, "prefix", None) or _get(_get(obj, "include_context"), "prefix") or ""
|
||||
own = own if isinstance(own, str) else ""
|
||||
if not _is_endpoint(obj) and not own and hasattr(obj, "routes"):
|
||||
own = getattr(obj, "path", "") or "" # Mount
|
||||
if own and not own.startswith(prefix or "\0"):
|
||||
|
||||
Reference in new issue
Block a user