Browse Source

feat: add check in websocket routes and support prefix for Starlette Routers

pull/5595/head
Irfanuddin 2 years ago
parent
commit
d11ed8208b
No known key found for this signature in database GPG Key ID: EDDB7CBA28F0477C
  1. 3
      fastapi/dependencies/utils.py
  2. 6
      fastapi/routing.py

3
fastapi/dependencies/utils.py

@ -757,5 +757,6 @@ def get_body_field(*, dependant: Dependant, name: str) -> Optional[ModelField]:
return final_field
def get_path_hash_val(path: str, methods: Optional[Set[str]]) -> str:
def get_path_hash_val(path: str, methods: Optional[Set[str]] = None) -> str:
methods = methods or {"GET"}
return f"path:{path};methods:{methods}"

6
fastapi/routing.py

@ -677,6 +677,10 @@ class APIRouter(routing.Router):
name=name,
dependency_overrides_provider=self.dependency_overrides_provider,
)
hash_val = get_path_hash_val(route.path)
if hash_val in self.added_routes:
raise RouteAlreadyExistsError(route.name)
self.added_routes.add(hash_val)
self.routes.append(route)
def websocket(
@ -781,7 +785,7 @@ class APIRouter(routing.Router):
)
elif isinstance(route, routing.Route):
methods = list(route.methods or [])
hash_val = get_path_hash_val(route.path, route.methods)
hash_val = get_path_hash_val(prefix + route.path, route.methods)
if hash_val in self.added_routes:
raise RouteAlreadyExistsError(route.name)
self.added_routes.add(hash_val)

Loading…
Cancel
Save