[FIX] base: prevent json decoder error for default values
JSONDecoderError occurs when users enters invalid JSON format data in 'Default Value' field inside 'User-defined Defaults' and wherever this field is being accessed to get default value this traceback will be generated. Steps to reproduce: 1) Install 'Contacts' module. 2) Open 'Settings' > 'Technical' > 'User-defined Defaults'. 3) Click on record 'Language' > 'EDIT' button > in 'Default Value' field enter any improper JSON format data (e.g 'Maa' : FI ) . 4) Now, open 'Contacts' module > click on 'CREATE' button and traceback would be generated. By applying this, it will check for proper JSON format. Sentry-4169062951 closes odoo/odoo#126932 X-original-commit: 71b32d9ea892c8a79c8eac51b9f394c92c44f45d Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
This commit is contained in:
@@ -18228,6 +18228,12 @@ msgstr ""
|
||||
msgid "Invalid 'group by' parameter"
|
||||
msgstr ""
|
||||
|
||||
#. module: base
|
||||
#: code:addons/base/models/ir_default.py:0
|
||||
#, python-format
|
||||
msgid "Invalid JSON format in Default Value field."
|
||||
msgstr ""
|
||||
|
||||
#. module: base
|
||||
#. odoo-python
|
||||
#: code:addons/base/models/ir_ui_view.py:0
|
||||
|
||||
@@ -22,6 +22,14 @@ class IrDefault(models.Model):
|
||||
condition = fields.Char('Condition', help="If set, applies the default upon condition.")
|
||||
json_value = fields.Char('Default Value (JSON format)', required=True)
|
||||
|
||||
@api.constrains('json_value')
|
||||
def _check_json_format(self):
|
||||
for record in self:
|
||||
try:
|
||||
json.loads(record.json_value)
|
||||
except json.JSONDecodeError:
|
||||
raise ValidationError(_('Invalid JSON format in Default Value field.'))
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
self.clear_caches()
|
||||
|
||||
@@ -143,3 +143,13 @@ class TestIrDefault(TransactionCase):
|
||||
IrDefault.with_context(allowed_company_ids=company_b_a.ids)._get_model_defaults('res.partner')['ref'],
|
||||
'CBDefault',
|
||||
)
|
||||
|
||||
def test_json_format_invalid(self):
|
||||
""" check the _check_json_format constraint """
|
||||
IrDefault = self.env['ir.default']
|
||||
field_id = self.env['ir.model.fields'].search([('model', '=', 'res.partner'), ('name', '=', 'ref')])
|
||||
with self.assertRaises(ValidationError):
|
||||
IrDefault.create({
|
||||
'field_id': field_id.id,
|
||||
'json_value': '{"name":"John", }',
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user