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

pull/2418/head
Laurent Mignon (ACSONE) 2018-08-22 12:02:35 +02:00 committed by Sébastien BEAU
parent 36d8adea96
commit 52d508ed9b
2 changed files with 23 additions and 12 deletions

View File

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

View File

@ -66,7 +66,7 @@ class TestParser(TransactionCase):
})
],
})
expected_json = [{
expected_json = {
u'lang': u'en_US',
u'comment': None,
u'credit_limit': 0.0,
@ -90,7 +90,23 @@ class TestParser(TransactionCase):
u'name': u'Sebatien Beau',
u'email': None
}]
}]
}
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)