Skip to content

[16.0][IMP]fastapi: enable multi-slash routes #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastapi/fastapi_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def dispatch(self, endpoint, args):
# don't parse the httprequest let starlette parse the stream
self.request.params = {} # dict(self.request.get_http_params(), **args)
environ = self._get_environ()
root_path = "/" + environ["PATH_INFO"].split("/")[1]
root_path = environ["PATH_INFO"]
# TODO store the env into contextvar to be used by the odoo_env
# depends method
with fastapi_app_pool.get_app(env=request.env, root_path=root_path) as app:
Expand Down
19 changes: 17 additions & 2 deletions fastapi/models/fastapi_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,24 @@
to all the running instances.
"""

@api.model
def get_endpoint(self, root_path):
# try to match the request url with the most similar endpoint
endpoints_by_length = self.search([]).sorted(
lambda fe: len(fe.root_path), reverse=True
)
endpoint = False
while endpoints_by_length:
candidate_endpoint = endpoints_by_length[0]
if root_path.startswith(candidate_endpoint.root_path):
endpoint = candidate_endpoint
break
endpoints_by_length -= candidate_endpoint

Check warning on line 225 in fastapi/models/fastapi_endpoint.py

View check run for this annotation

Codecov / codecov/patch

fastapi/models/fastapi_endpoint.py#L225

Added line #L225 was not covered by tests
return endpoint or None

@api.model
def get_app(self, root_path):
record = self.search([("root_path", "=", root_path)])
record = self.get_endpoint(root_path)
if not record:
return None
app = FastAPI()
Expand All @@ -238,7 +253,7 @@
@api.model
@tools.ormcache("root_path")
def get_uid(self, root_path):
record = self.search([("root_path", "=", root_path)])
record = self.get_endpoint(root_path)
if not record:
return None
return record.user_id.id
Expand Down