From 3ca15ceada6c48ac9e6a87f41c41719dcfe4ca89 Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Wed, 22 Aug 2018 12:02:35 +0200 Subject: [PATCH] base_jsonify: Output 'false' into json only for boolean fields --- base_jsonify/models/models.py | 13 ++++--------- base_jsonify/tests/test_get_parser.py | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/base_jsonify/models/models.py b/base_jsonify/models/models.py index 5a6afc7a2..d4b9b678d 100644 --- a/base_jsonify/models/models.py +++ b/base_jsonify/models/models.py @@ -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 diff --git a/base_jsonify/tests/test_get_parser.py b/base_jsonify/tests/test_get_parser.py index e73267fb2..80d5b354a 100644 --- a/base_jsonify/tests/test_get_parser.py +++ b/base_jsonify/tests/test_get_parser.py @@ -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)