base_jsonify: Output 'false' into json only for boolean fields

pull/1704/head
Laurent Mignon (ACSONE) 2018-08-22 12:02:35 +02:00 committed by laurent.corron
parent cfabca6d04
commit 6be10baebd
2 changed files with 23 additions and 12 deletions

View File

@ -50,12 +50,6 @@ class Base(models.AbstractModel):
""" """
result = [] result = []
empty_value = {
'char': None,
'int': None,
# 'float': None, TODO: 0.0 != False
'text': None,
}
for rec in self: for rec in self:
res = {} res = {}
@ -73,8 +67,9 @@ class Base(models.AbstractModel):
else: else:
raise UserError(_('Wrong parser configuration')) raise UserError(_('Wrong parser configuration'))
else: else:
res[json_key] = rec[field_name] value = rec[field_name]
if not res[json_key] and field_type in empty_value: if value is False and field_type != 'boolean':
res[json_key] = empty_value[field_type] value = None
res[json_key] = value
result.append(res) result.append(res)
return result return result

View File

@ -66,7 +66,7 @@ class TestParser(TransactionCase):
}) })
], ],
}) })
expected_json = [{ expected_json = {
u'lang': u'en_US', u'lang': u'en_US',
u'comment': None, u'comment': None,
u'credit_limit': 0.0, u'credit_limit': 0.0,
@ -90,7 +90,23 @@ class TestParser(TransactionCase):
u'name': u'Sebatien Beau', u'name': u'Sebatien Beau',
u'email': None u'email': None
}] }]
}] }
json_partner = partner.jsonify(parser) json_partner = partner.jsonify(parser)
self.assertDictEqual(json_partner[0], expected_json[0]) self.assertDictEqual(json_partner[0], expected_json)
json_partner = partner.jsonify(parser)
self.assertDictEqual(json_partner[0], expected_json)
# Check that only boolean fields have boolean values into json
# By default if a field is not set into Odoo, the value is always False
# This value is not the expected one into the json
partner.write({'child_ids': [(6, 0, [])],
'active': False,
'lang': False})
json_partner = partner.jsonify(parser)
expected_json['active'] = False
expected_json['lang'] = None
expected_json['children'] = []
self.assertDictEqual(json_partner[0], expected_json)