Skip to content
Snippets Groups Projects
Commit 675ed5d9 authored by Pierre Masereel's avatar Pierre Masereel Committed by Raphael Collet
Browse files

[FIX] expression: interpret `(X, 'child_of', False)` as false

Interpret such domains that do not really make sense as falsy, and log a
warning when it occurs.
parent 53300648
No related branches found
No related tags found
No related merge requests found
......@@ -134,6 +134,15 @@ class TestExpression(TransactionCase):
cats = Category.search([('id', 'child_of', categ_1.ids)])
self.assertEqual(len(cats), 1)
# test hierarchical search in m2m with an empty list
cats = Category.search([('id', 'child_of', [])])
self.assertEqual(len(cats), 0)
# test hierarchical search in m2m with 'False' value
with self.assertLogs('odoo.osv.expression'):
cats = Category.search([('id', 'child_of', False)])
self.assertEqual(len(cats), 0)
# test hierarchical search in m2m with parent id (list of ids)
cats = Category.search([('id', 'parent_of', categ_1.ids)])
self.assertEqual(len(cats), 3)
......@@ -154,6 +163,15 @@ class TestExpression(TransactionCase):
cats = Category.search([('id', 'parent_of', categ_root.ids)])
self.assertEqual(len(cats), 1)
# test hierarchical search in m2m with an empty list
cats = Category.search([('id', 'parent_of', [])])
self.assertEqual(len(cats), 0)
# test hierarchical search in m2m with 'False' value
with self.assertLogs('odoo.osv.expression'):
cats = Category.search([('id', 'parent_of', False)])
self.assertEqual(len(cats), 0)
def test_10_equivalent_id(self):
# equivalent queries
Currency = self.env['res.currency']
......
......@@ -712,7 +712,7 @@ class expression(object):
"""
cr, uid, context = self.root_model.env.args
def to_ids(value, comodel):
def to_ids(value, comodel, leaf):
""" Normalize a single id or name, or a list of those, into a list of ids
:param {int,long,basestring,list,tuple} value:
if int, long -> return [value]
......@@ -727,6 +727,12 @@ class expression(object):
elif value and isinstance(value, (tuple, list)) and all(isinstance(item, pycompat.string_types) for item in value):
names = value
elif isinstance(value, pycompat.integer_types):
if not value:
# given this nonsensical domain, it is generally cheaper to
# interpret False as [], so that "X child_of False" will
# match nothing
_logger.warning("Unexpected domain [%s], interpreted as False", leaf)
return []
return [value]
if names:
return list({
......@@ -853,7 +859,7 @@ class expression(object):
push(leaf)
elif left == 'id' and operator in HIERARCHY_FUNCS:
ids2 = to_ids(right, model)
ids2 = to_ids(right, model, leaf.leaf)
dom = HIERARCHY_FUNCS[operator](left, ids2, model)
for dom_leaf in reversed(dom):
new_leaf = create_substitution_leaf(leaf, dom_leaf, model)
......@@ -931,7 +937,7 @@ class expression(object):
# Applying recursivity on field(one2many)
elif field.type == 'one2many' and operator in HIERARCHY_FUNCS:
ids2 = to_ids(right, comodel)
ids2 = to_ids(right, comodel, leaf.leaf)
if field.comodel_name != model._name:
dom = HIERARCHY_FUNCS[operator](left, ids2, comodel, prefix=field.comodel_name)
else:
......@@ -992,7 +998,7 @@ class expression(object):
if operator in HIERARCHY_FUNCS:
# determine ids2 in comodel
ids2 = to_ids(right, comodel)
ids2 = to_ids(right, comodel, leaf.leaf)
domain = HIERARCHY_FUNCS[operator]('id', ids2, comodel)
ids2 = comodel.search(domain).ids
......@@ -1033,7 +1039,7 @@ class expression(object):
elif field.type == 'many2one':
if operator in HIERARCHY_FUNCS:
ids2 = to_ids(right, comodel)
ids2 = to_ids(right, comodel, leaf.leaf)
if field.comodel_name != model._name:
dom = HIERARCHY_FUNCS[operator](left, ids2, comodel, prefix=field.comodel_name)
else:
......
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