Skip to content

Commit f7dda08

Browse files
committed
[IMP]fastapi: enable multi-slash routes
1 parent 1e73351 commit f7dda08

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

fastapi/fastapi_dispatcher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def dispatch(self, endpoint, args):
2727
# don't parse the httprequest let starlette parse the stream
2828
self.request.params = {} # dict(self.request.get_http_params(), **args)
2929
environ = self._get_environ()
30-
root_path = "/" + environ["PATH_INFO"].split("/")[1]
30+
root_path = environ["PATH_INFO"]
3131
# TODO store the env into contextvar to be used by the odoo_env
3232
# depends method
3333
with fastapi_app_pool.get_app(env=request.env, root_path=root_path) as app:

fastapi/models/fastapi_endpoint.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,24 @@ def _reset_app_cache_marker(self):
210210
to all the running instances.
211211
"""
212212

213+
@api.model
214+
def get_endpoint(self, root_path):
215+
# try to match the request url with the most similar endpoint
216+
endpoints_by_length = self.search([]).sorted(
217+
lambda fe: len(fe.root_path), reverse=True
218+
)
219+
endpoint = False
220+
while endpoints_by_length:
221+
candidate_endpoint = endpoints_by_length[0]
222+
if root_path.startswith(candidate_endpoint.root_path):
223+
endpoint = candidate_endpoint
224+
break
225+
endpoints_by_length -= candidate_endpoint
226+
return endpoint or None
227+
213228
@api.model
214229
def get_app(self, root_path):
215-
record = self.search([("root_path", "=", root_path)])
230+
record = self.get_endpoint(root_path)
216231
if not record:
217232
return None
218233
app = FastAPI()
@@ -238,7 +253,7 @@ def _clear_fastapi_exception_handlers(self, app: FastAPI) -> None:
238253
@api.model
239254
@tools.ormcache("root_path")
240255
def get_uid(self, root_path):
241-
record = self.search([("root_path", "=", root_path)])
256+
record = self.get_endpoint(root_path)
242257
if not record:
243258
return None
244259
return record.user_id.id

0 commit comments

Comments
 (0)