diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 367dea5f5..d5d6f13ea 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,6 @@ exclude: | (?x) # NOT INSTALLABLE ADDONS - ^base_rest/| ^base_rest_auth_api_key/| ^base_rest_pydantic/| ^extendable/| diff --git a/base_rest/README.rst b/base_rest/README.rst index 5f93be562..b06072c92 100644 --- a/base_rest/README.rst +++ b/base_rest/README.rst @@ -11,7 +11,7 @@ Base Rest !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:198b97806a0a8c480e6c8f26f3ce3199cba9978f8d152ecf93c80091323d1805 + !! source digest: sha256:9e4588157b2a0c1f427679ba2a04ae87646f52c849579fafb07a52da6f7f577b !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png @@ -21,13 +21,13 @@ Base Rest :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html :alt: License: LGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github - :target: https://github.com/OCA/rest-framework/tree/18.0/base_rest + :target: https://github.com/OCA/rest-framework/tree/19.0/base_rest :alt: OCA/rest-framework .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-base_rest + :target: https://translation.odoo-community.org/projects/rest-framework-19-0/rest-framework-19-0-base_rest :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png - :target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=18.0 + :target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=19.0 :alt: Try me on Runboat |badge1| |badge2| |badge3| |badge4| |badge5| @@ -404,7 +404,7 @@ Bug Tracker Bugs are tracked on `GitHub Issues `_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -421,6 +421,7 @@ Contributors - Laurent Mignon - Sébastien Beau +- Michael Tietz (MT Software) Maintainers ----------- @@ -435,6 +436,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -This module is part of the `OCA/rest-framework `_ project on GitHub. +This module is part of the `OCA/rest-framework `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_rest/__manifest__.py b/base_rest/__manifest__.py index 755c27fe4..3fe61a2ed 100644 --- a/base_rest/__manifest__.py +++ b/base_rest/__manifest__.py @@ -6,10 +6,10 @@ "summary": """ Develop your own high level REST APIs for Odoo thanks to this addon. """, - "version": "18.0.1.1.1", + "version": "19.0.1.0.0", "development_status": "Beta", "license": "LGPL-3", - "author": "ACSONE SA/NV, " "Odoo Community Association (OCA)", + "author": "ACSONE SA/NV, Odoo Community Association (OCA)", "maintainers": [], "website": "https://github.com/OCA/rest-framework", "depends": ["component", "web"], @@ -20,8 +20,6 @@ "assets": { "web.assets_frontend": [ "base_rest/static/src/scss/base_rest.scss", - "base_rest/static/src/js/swagger_ui.js", - "base_rest/static/src/js/swagger.js", ], }, "external_dependencies": { @@ -33,5 +31,5 @@ "apispec", ] }, - "installable": False, + "installable": True, } diff --git a/base_rest/components/service.py b/base_rest/components/service.py index b8611bcf2..2a449d37d 100644 --- a/base_rest/components/service.py +++ b/base_rest/components/service.py @@ -3,6 +3,7 @@ import logging +import os from werkzeug.exceptions import NotFound @@ -17,6 +18,23 @@ _logger = logging.getLogger(__name__) +# Disable logging for deprecation. +# You might have big projects, with tons of services with no specific decorator, +# that are well tested and very stable and you don't want to touch them. +# Especially if you plan already to move to v16. +# Since this logging can bloat your CI and your log collecting tools, +# here is a way to prevent that and get only a msg at boot. +DISABLE_DEPRECATE_LOG_KEY = "REST_API_METHOD_FIX_DECORATOR_DEPRECATE_LOG_DISABLE" +DISABLE_DEPRECATE_LOG = os.getenv(DISABLE_DEPRECATE_LOG_KEY) +if DISABLE_DEPRECATE_LOG: + msg = ( + "%s enabled: " + "implicit service methods are deprecated. " + "Disable this env key and enable debug level to have more details." + ) + _logger.warning(msg, DISABLE_DEPRECATE_LOG_KEY) + + def to_int(val): # The javascript VM ducktape only use float and so pass float # to the api, the werkzeug request interpret params as unicode @@ -127,15 +145,16 @@ def _prepare_response(self, method, result): return result routing = getattr(method, ROUTING_DECORATOR_ATTR, None) output_param = routing["output_param"] - if not output_param: + if output_param: + return output_param.to_response(self, result) + elif not output_param and not DISABLE_DEPRECATE_LOG: _logger.warning( "DEPRECATED: You must define an output schema for method %s " "in service %s", method_name, self._name, ) - return result - return output_param.to_response(self, result) + return result def dispatch(self, method_name, *args, params=None): """ @@ -197,8 +216,7 @@ def _get_openapi_default_responses(self): return { "400": {"description": "One of the given parameter is not valid"}, "401": { - "description": "The user is not authorized. Authentication " - "is required" + "description": "The user is not authorized. Authentication is required" }, "404": {"description": "Requested resource not found"}, "403": { diff --git a/base_rest/http.py b/base_rest/http.py index 71b3c8a18..ad48aaf31 100644 --- a/base_rest/http.py +++ b/base_rest/http.py @@ -3,6 +3,7 @@ # @author Sébastien BEAU # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +import configparser import datetime import decimal import json @@ -29,8 +30,8 @@ ValidationError, ) from odoo.http import ( - CSRF_FREE_METHODS, MISSING_CSRF_WARNING, + SAFE_HTTP_METHODS, Dispatcher, SessionExpiredException, request, @@ -63,6 +64,17 @@ def default(self, obj): # pylint: disable=E0202,arguments-differ BLACKLISTED_LOG_PARAMS = ("password",) +def _is_dev_mode(): + """Read ``dev_mode`` in the ``[base_rest]`` section of the config file. + + Since 19.0, Odoo only reads the ``[options]`` section of this file. + """ + parser = configparser.RawConfigParser() + if config.get("config"): + parser.read(config["config"]) + return parser.getboolean("base_rest", "dev_mode", fallback=False) + + def wrapJsonException(exception, include_description=False, extra_info=None): """Wrap exceptions to be rendered as JSON. @@ -77,7 +89,7 @@ def wrapJsonException(exception, include_description=False, extra_info=None): def get_body(environ=None, scope=None): res = {"code": exception.code, "name": escape(exception.name)} description = exception.get_description(environ) - if config.get_misc("base_rest", "dev_mode"): + if _is_dev_mode(): # return exception info only if base_rest is in dev_mode res.update({"traceback": exception.traceback, "description": description}) elif include_description: @@ -162,7 +174,7 @@ def dispatch(self, endpoint, args): # Check for CSRF token for relevant requests if ( - self.request.httprequest.method not in CSRF_FREE_METHODS + self.request.httprequest.method not in SAFE_HTTP_METHODS and endpoint.routing.get("csrf", True) ): token = params.pop("csrf_token", None) diff --git a/base_rest/models/rest_service_registration.py b/base_rest/models/rest_service_registration.py index 60e06fd3b..a067330b7 100644 --- a/base_rest/models/rest_service_registration.py +++ b/base_rest/models/rest_service_registration.py @@ -16,8 +16,8 @@ from werkzeug.routing import Map, Rule -import odoo from odoo import http, models +from odoo.modules import module_graph from odoo.addons.component.core import WorkContext @@ -189,10 +189,10 @@ def build_registry(self, services_registry, states=None, exclude_addons=None): # dependencies to ensure that controllers defined in a more # specialized addon and overriding more generic one takes precedences # on the generic one into the registry - graph = odoo.modules.graph.Graph() - graph.add_module(self.env.cr, "base") + graph = module_graph.ModuleGraph(self.env.cr) + graph.extend(["base"]) - query = "SELECT name " "FROM ir_module_module " "WHERE state IN %s " + query = "SELECT name FROM ir_module_module WHERE state IN %s " params = [tuple(states)] if exclude_addons: query += " AND name NOT IN %s " @@ -200,7 +200,7 @@ def build_registry(self, services_registry, states=None, exclude_addons=None): self.env.cr.execute(query, params) module_list = [name for (name,) in self.env.cr.fetchall() if name not in graph] - graph.add_modules(self.env.cr, module_list) + graph.extend(module_list) for module in graph: self.load_services(module.name, services_registry) diff --git a/base_rest/restapi.py b/base_rest/restapi.py index c9cf2c0e7..81561e780 100644 --- a/base_rest/restapi.py +++ b/base_rest/restapi.py @@ -218,13 +218,13 @@ def from_params(self, service, params): validator = self.get_cerberus_validator(service, "input") if validator.validate(params): return validator.document - raise UserError(service.env._("BadRequest %s") % validator.errors) + raise UserError(service.env._("BadRequest %s", str(validator.errors))) def to_response(self, service, result): validator = self.get_cerberus_validator(service, "output") if validator.validate(result): return validator.document - raise SystemError(service.env._("Invalid Response %s") % validator.errors) + raise SystemError(service.env._("Invalid Response %s", str(validator.errors))) def to_openapi_query_parameters(self, service, spec): json_schema = self.to_json_schema(service, spec, "input") @@ -276,7 +276,7 @@ def get_cerberus_validator(self, service, direction): if isinstance(schema, dict): return Validator(schema, purge_unknown=True) raise Exception( - service.env._("Unable to get cerberus schema from %s") % self._schema + service.env._("Unable to get cerberus schema from %s", str(self._schema)) ) def to_json_schema(self, service, spec, direction): @@ -412,7 +412,11 @@ def from_params(self, service, params): ) # multipart ony sends its parts as string except json.JSONDecodeError as error: raise ValidationError( - service.env._(f"{key}'s JSON content is malformed: {error}") + service.env._( + "%(key)s's JSON content is malformed: %(error)s", + key=key, + error=str(error), + ) ) from error param = part.from_params(service, json_param) params[key] = param diff --git a/base_rest/static/description/index.html b/base_rest/static/description/index.html index 11068ef2a..32e30ab8d 100644 --- a/base_rest/static/description/index.html +++ b/base_rest/static/description/index.html @@ -3,7 +3,7 @@ -README.rst +Base Rest