From d87a299a820bd9b13fa74e8a900db0e9b7fddb7e Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Tue, 20 Feb 2024 10:27:27 +0100 Subject: [PATCH] [FIX] fastapi: Compatibility with latest Odoo From https://github.com/odoo/odoo/commit/cb1d057dcab28cb0b0487244ba99231ee292502e, the orignal werkzeug request is wrapped in a dedicated class to keep under control the attributes developers use. This change add code to make the fastapi addon working with version including this last change and prior version refs #414 --- fastapi/fastapi_dispatcher.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fastapi/fastapi_dispatcher.py b/fastapi/fastapi_dispatcher.py index e2ff180e..a74c44b4 100644 --- a/fastapi/fastapi_dispatcher.py +++ b/fastapi/fastapi_dispatcher.py @@ -48,8 +48,19 @@ def _make_response(self, status_mapping, headers_tuple, content): self.headers = dict(headers_tuple) def _get_environ(self): - environ = self.request.httprequest.environ - environ["wsgi.input"] = self.request.httprequest._get_stream_for_parsing() + try: + # normal case after + # https://github.com/odoo/odoo/commit/cb1d057dcab28cb0b0487244ba99231ee292502e + httprequest = self.request.httprequest._HTTPRequest__wrapped + except AttributeError: + # fallback for older odoo versions + # The try except is the most efficient way to handle this + # as we expect that most of the time the attribute will be there + # and this code will no more be executed if it runs on an up to + # date odoo version. (EAFP: Easier to Ask for Forgiveness than Permission) + httprequest = self.request.httprequest + environ = httprequest.environ + environ["wsgi.input"] = httprequest._get_stream_for_parsing() return environ @contextmanager