Skip to content

Commit 4eea801

Browse files
committed
[IMP]fastapi: expose FastAPI docs only if field is set
1 parent a0da317 commit 4eea801

6 files changed

Lines changed: 199 additions & 145 deletions

File tree

fastapi/README.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. image:: https://odoo-community.org/readme-banner-image
2-
:target: https://odoo-community.org/get-involved?utm_source=readme
3-
:alt: Odoo Community Association
4-
51
============
62
Odoo FastAPI
73
============
@@ -17,7 +13,7 @@ Odoo FastAPI
1713
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1814
:target: https://odoo-community.org/page/development-status
1915
:alt: Beta
20-
.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png
16+
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
2117
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
2218
:alt: License: LGPL-3
2319
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github
@@ -251,6 +247,12 @@ Now, you can start your Odoo server, install your addon and create a new endpoin
251247
instance for your app. Once it's done click on the docs url to access the
252248
interactive documentation of your app.
253249

250+
**Note**: FastAPI automatically exposes three services to easily interact with
251+
the APIs and their structure (Swagger, Redoc, OpenAPI).
252+
When exposing your Odoo instance to the open internet you might want to disable
253+
these services. To do so toggle the "Expose FastAPI docs" checkbox in the FastAPI
254+
Endpoint form view.
255+
254256
Before trying to test your app, you need to define on the endpoint instance the
255257
user that will be used to run the app. You can do it by setting the **'user_id'**
256258
field. This information is the most important one because it's the basis for

fastapi/models/fastapi_endpoint.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class FastapiEndpoint(models.Model):
6565
"unexpecteed disk space consumption.",
6666
default=True,
6767
)
68+
expose_doc_urls = fields.Boolean("Expose FastAPI docs", default=True)
6869

6970
@api.depends("root_path")
7071
def _compute_root_path(self):
@@ -150,7 +151,7 @@ def _handle_route_updates(self, vals):
150151
@api.model
151152
def _fastapi_app_fields(self) -> List[str]:
152153
"""The list of fields requiring to refresh the fastapi app if modified"""
153-
return []
154+
return ["expose_doc_urls"]
154155

155156
def _make_routing_rule(self, options=None):
156157
"""Generator of rule"""
@@ -258,12 +259,15 @@ def _get_app_dependencies_overrides(self) -> Dict[Callable, Callable]:
258259

259260
def _prepare_fastapi_app_params(self) -> Dict[str, Any]:
260261
"""Return the params to pass to the Fast API app constructor"""
261-
return {
262+
to_return = {
262263
"title": self.name,
263264
"description": self.description,
264265
"middleware": self._get_fastapi_app_middlewares(),
265266
"dependencies": self._get_fastapi_app_dependencies(),
266267
}
268+
if not self.expose_doc_urls:
269+
to_return |= {"docs_url": None, "redoc_url": None, "openapi_url": None}
270+
return to_return
267271

268272
def _get_fastapi_routers(self) -> List[APIRouter]:
269273
"""Return the api routers to use for the instance.

fastapi/readme/USAGE.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ Now, you can start your Odoo server, install your addon and create a new endpoin
170170
instance for your app. Once it's done click on the docs url to access the
171171
interactive documentation of your app.
172172

173+
**Note**: FastAPI automatically exposes three services to easily interact with
174+
the APIs and their structure (Swagger, Redoc, OpenAPI).
175+
When exposing your Odoo instance to the open internet you might want to disable
176+
these services. To do so toggle the "Expose FastAPI docs" checkbox in the FastAPI
177+
Endpoint form view.
178+
173179
Before trying to test your app, you need to define on the endpoint instance the
174180
user that will be used to run the app. You can do it by setting the **'user_id'**
175181
field. This information is the most important one because it's the basis for

0 commit comments

Comments
 (0)