Skip to content

Commit b76cd6f

Browse files
committed
Merge PR #610 into 19.0
Signed-off-by lmignon
2 parents 4bc043d + 7537bbb commit b76cd6f

7 files changed

Lines changed: 89 additions & 13 deletions

File tree

.oca/oca-port/blacklist/fastapi.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"OCA/rest-framework#440": "(auto) Nothing to port from PR #440",
88
"OCA/rest-framework#476": "(auto) Nothing to port from PR #476",
99
"OCA/rest-framework#364": "(auto) Nothing to port from PR #364",
10-
"OCA/rest-framework#486": "(auto) Nothing to port from PR #486"
10+
"OCA/rest-framework#486": "(auto) Nothing to port from PR #486",
11+
"OCA/rest-framework#607": "(auto) Nothing to port from PR #607"
1112
}
1213
}

fastapi/dependencies.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2022 ACSONE SA/NV
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/LGPL).
33

4-
from typing import TYPE_CHECKING, Annotated
4+
from typing import Annotated
55

66
from odoo.api import Environment
77
from odoo.exceptions import AccessDenied
@@ -13,11 +13,9 @@
1313
from fastapi.security import HTTPBasic, HTTPBasicCredentials
1414

1515
from .context import odoo_env_ctx
16+
from .models.fastapi_endpoint import FastapiEndpoint
1617
from .schemas import Paging
1718

18-
if TYPE_CHECKING:
19-
from .models.fastapi_endpoint import FastapiEndpoint
20-
2119

2220
def company_id() -> int | None:
2321
"""This method may be overriden by the FastAPI app to set the allowed company

fastapi/error_handlers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ def convert_exception_to_status_body(exc: Exception) -> tuple[int, dict]:
2929
status_code = exc.status_code
3030
details = exc.detail
3131
elif isinstance(exc, RequestValidationError):
32-
status_code = status.HTTP_422_UNPROCESSABLE_CONTENT
32+
# Use integer status code to supress starlette >= 0.48 deprecation warning
33+
# See: https://github.com/fastapi/fastapi/pull/14077
34+
# status_code = status.HTTP_422_UNPROCESSABLE_CONTENT
35+
status_code = 422
3336
details = jsonable_encoder(exc.errors())
3437
elif isinstance(exc, WebSocketRequestValidationError):
3538
status_code = status.WS_1008_POLICY_VIOLATION

fastapi/models/fastapi_endpoint.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
from fastapi import APIRouter, Depends, FastAPI
1717

18-
from .. import dependencies
1918
from ..middleware import ASGIMiddleware
2019

2120
_logger = logging.getLogger(__name__)
@@ -311,9 +310,12 @@ def _get_app(self) -> FastAPI:
311310
return app
312311

313312
def _get_app_dependencies_overrides(self) -> dict[Callable, Callable]:
313+
# Import here to avoid circular import while waiting for lazy imports
314+
from ..dependencies import company_id, fastapi_endpoint_id
315+
314316
return {
315-
dependencies.fastapi_endpoint_id: partial(lambda a: a, self.id),
316-
dependencies.company_id: partial(lambda a: a, self.company_id.id),
317+
fastapi_endpoint_id: partial(lambda a: a, self.id),
318+
company_id: partial(lambda a: a, self.company_id.id),
317319
}
318320

319321
def _prepare_fastapi_app_params(self) -> dict[str, Any]:
@@ -338,7 +340,10 @@ def _get_fastapi_app_middlewares(self) -> list[Middleware]:
338340

339341
def _get_fastapi_app_dependencies(self) -> list[Depends]:
340342
"""Return the dependencies to use for the fastapi app."""
341-
return [Depends(dependencies.accept_language)]
343+
# Import here to avoid circular import too
344+
from ..dependencies import accept_language
345+
346+
return [Depends(accept_language)]
342347

343348
# test utility
344349
@api.model
-34.1 KB
Loading
Lines changed: 71 additions & 0 deletions
Loading

fastapi/tests/test_fastapi.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ def test_request_validation_error(self) -> None:
157157
route = "/fastapi_demo/demo/exception?exception_type=BAD&error_message="
158158
response = self.url_open(route, timeout=200)
159159
mocked_commit.assert_not_called()
160-
self.assertEqual(
161-
response.status_code, status.HTTP_422_UNPROCESSABLE_CONTENT
162-
)
160+
self.assertEqual(response.status_code, 422)
163161

164162
def test_no_commit_on_exception(self) -> None:
165163
# this test check that the way we mock the cursor is working as expected

0 commit comments

Comments
 (0)