Skip to content
Snippets Groups Projects
Commit b57bed75 authored by Mohit Beniwal's avatar Mohit Beniwal
Browse files

[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#126239

X-original-commit: 0e6e322d
Signed-off-by: default avatarRémy Voet (ryv) <ryv@odoo.com>
parent 56cf4050
No related branches found
No related tags found
No related merge requests found
......@@ -13002,7 +13002,12 @@ msgid "Invalid 'group by' parameter"
msgstr ""
 
#. module: base
#. odoo-python
#: code:addons/base/models/ir_default.py:0
#, python-format
msgid "Invalid JSON format in Default Value field."
msgstr ""
#. module: base
#: code:addons/models.py:0
#, python-format
msgid "Invalid aggregation function %r."
......
......@@ -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", }',
})
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