Skip to content
Snippets Groups Projects
Commit dd3bf0df authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

:hammer: fastjsonschema is now an optional dependency

parent d1fa5abc
No related branches found
No related tags found
No related merge requests found
...@@ -6,3 +6,12 @@ ...@@ -6,3 +6,12 @@
TEST_OPTIONS: --test-tags=odoo_addons_mail TEST_OPTIONS: --test-tags=odoo_addons_mail
# Also install mail to run mail dependant tets # Also install mail to run mail dependant tets
ODOO_SCRIPTS_MODULE_LIST: mail ODOO_SCRIPTS_MODULE_LIST: mail
mypy:
variables:
INSTALL_SECTION: "[test,schema-validation]"
test-schema-validation:
extends: test
variables:
INSTALL_SECTION: "[test,schema-validation]"
Changelog Changelog
========= =========
18.0.6.0.0
----------
fastjsonschema is now an optional dependency. It is listed in the dependencies of section *schema-validation*.
If validation needs to be done and the library is missing, an exception will be raised.
18.0.5.0.0 18.0.5.0.0
---------- ----------
......
...@@ -18,3 +18,5 @@ ...@@ -18,3 +18,5 @@
|maturity| |license| |ruff| |prettier| |maturity| |license| |ruff| |prettier|
Convert odoo records to/from plain data structures. Convert odoo records to/from plain data structures.
section *schema-validation* need to be installed if validation is needed.
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
"name": "Converter", "name": "Converter",
"license": "AGPL-3", "license": "AGPL-3",
"summary": "Convert odoo records to/from plain data structures.", "summary": "Convert odoo records to/from plain data structures.",
"version": "18.0.5.0.0", "version": "18.0.6.0.0",
"category": "Hidden", "category": "Hidden",
"author": "XCG Consulting", "author": "XCG Consulting",
"website": "https://orbeet.io/", "website": "https://orbeet.io/",
"depends": ["base"], "depends": ["base"],
"installable": True, "installable": True,
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
"category": "Hidden", "category": "Hidden",
"author": "XCG Consulting", "author": "XCG Consulting",
"website": "https://orbeet.io/", "website": "https://orbeet.io/",
"depends": ["base"], "depends": ["base"],
"installable": True, "installable": True,
"external_dependencies": {"python": ["fastjsonschema"]},
} }
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
from collections.abc import Iterable, Mapping from collections.abc import Iterable, Mapping
from typing import Any, Final from typing import Any, Final
import fastjsonschema # type: ignore[import-untyped]
from odoo import _, api, models # type: ignore[import-untyped] from odoo import _, api, models # type: ignore[import-untyped]
from odoo.exceptions import UserError # type: ignore[import-untyped] from odoo.exceptions import UserError # type: ignore[import-untyped]
...@@ -39,6 +38,14 @@ ...@@ -39,6 +38,14 @@
) )
from .validate import NotInitialized, Validation, Validator from .validate import NotInitialized, Validation, Validator
try:
from fastjsonschema import JsonSchemaException # type: ignore[import-untyped]
except ImportError:
# Ignore no-redef, added for compatibility
class JsonSchemaException(Exception): # type: ignore[no-redef]
"""Custom error in case of missing optional requirement"""
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
...@@ -129,7 +136,7 @@ ...@@ -129,7 +136,7 @@
if self.validator: if self.validator:
try: try:
self.validator.validate(self._jsonschema, message_data) self.validator.validate(self._jsonschema, message_data)
except (NotInitialized, fastjsonschema.JsonSchemaException): except (NotInitialized, JsonSchemaException):
_logger.warning("Validation failed", exc_info=True) _logger.warning("Validation failed", exc_info=True)
if self.validation == Validation.STRICT: if self.validation == Validation.STRICT:
raise raise
...@@ -152,7 +159,7 @@ ...@@ -152,7 +159,7 @@
if self.validator: if self.validator:
try: try:
self.validator.validate(self._jsonschema, message_value) self.validator.validate(self._jsonschema, message_value)
except (NotInitialized, fastjsonschema.JsonSchemaException): except (NotInitialized, JsonSchemaException):
_logger.warning("Validation failed", exc_info=True) _logger.warning("Validation failed", exc_info=True)
if self.validation == Validation.STRICT: if self.validation == Validation.STRICT:
raise raise
......
...@@ -15,6 +15,6 @@ ...@@ -15,6 +15,6 @@
"Framework :: Odoo :: 18.0", "Framework :: Odoo :: 18.0",
"License :: OSI Approved :: GNU Affero General Public License v3", "License :: OSI Approved :: GNU Affero General Public License v3",
] ]
dependencies = ["odoo==18.0.*", "fastjsonschema"] dependencies = ["odoo==18.0.*"]
[project.optional-dependencies] [project.optional-dependencies]
...@@ -19,5 +19,6 @@ ...@@ -19,5 +19,6 @@
[project.optional-dependencies] [project.optional-dependencies]
schema-validation = ["fastjsonschema"]
doc = ["sphinx"] doc = ["sphinx"]
test = [] test = []
......
...@@ -18,9 +18,10 @@ ...@@ -18,9 +18,10 @@
# #
############################################################################## ##############################################################################
import json import json
from unittest import skipUnless
from odoo import tests # type: ignore[import-untyped] from odoo import tests # type: ignore[import-untyped]
from ..validate import Validator from ..validate import Validator
...@@ -21,9 +22,10 @@ ...@@ -21,9 +22,10 @@
from odoo import tests # type: ignore[import-untyped] from odoo import tests # type: ignore[import-untyped]
from ..validate import Validator from ..validate import Validator
@skipUnless(tests.can_import("fastjsonschema"), "fastjsonschema module not available")
class TestValidate(tests.TransactionCase): class TestValidate(tests.TransactionCase):
def test_validate(self): def test_validate(self):
validator = Validator( validator = Validator(
......
...@@ -25,6 +25,5 @@ ...@@ -25,6 +25,5 @@
from importlib import import_module from importlib import import_module
from typing import Any, LiteralString from typing import Any, LiteralString
import fastjsonschema # type: ignore[import-untyped]
from odoo.exceptions import UserError # type: ignore[import-untyped] from odoo.exceptions import UserError # type: ignore[import-untyped]
...@@ -29,5 +28,12 @@ ...@@ -29,5 +28,12 @@
from odoo.exceptions import UserError # type: ignore[import-untyped] from odoo.exceptions import UserError # type: ignore[import-untyped]
_fastjsonschema: None | Exception = None
try:
import fastjsonschema # type: ignore[import-untyped]
except ImportError as e:
_fastjsonschema = e
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
...@@ -101,6 +107,8 @@ ...@@ -101,6 +107,8 @@
# Prepare validators for each schema. We add an HTTPS handler that # Prepare validators for each schema. We add an HTTPS handler that
# points back to our schema definition cache built above. # points back to our schema definition cache built above.
for schema_id, schema in schemas.items(): for schema_id, schema in schemas.items():
if _fastjsonschema is not None:
raise _fastjsonschema
self.validators[schema_id] = fastjsonschema.compile( self.validators[schema_id] = fastjsonschema.compile(
schema, schema,
handlers={"https": lambda uri: schemas[uri]}, handlers={"https": lambda uri: schemas[uri]},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment