[IMP] core: add warning for malformed aggregate specification

_read_group doesn't raise an error when we have an aggregate
specification like `order_id.create_date:min`, instead it silently
ignores the `.create_date` part.

We only add a warning in the stable version to avoid breaking any
change.

closes odoo/odoo#158777

Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
This commit is contained in:
Rémy Voet (ryv)
2024-03-22 16:49:49 +00:00
parent b4ae7c8425
commit 3c7db87ade
2 changed files with 13 additions and 5 deletions
@@ -312,19 +312,22 @@ class TestPrivateReadGroup(common.TransactionCase):
)
def test_malformed_params(self):
Model = self.env['test_read_group.fill_temporal']
Model = self.env['test_read_group.order.line']
# Test malformed groupby clause
with self.assertRaises(ValueError):
Model._read_group([], ['date:bad_granularity'])
Model._read_group([], ['create_date:bad_granularity'])
with self.assertRaises(ValueError):
Model._read_group([], ['Other stuff date:week'])
Model._read_group([], ['Other stuff create_date:week'])
with self.assertRaises(ValueError):
Model._read_group([], ['date']) # No granularity
Model._read_group([], ['create_date']) # No granularity
with self.assertRaises(ValueError):
Model._read_group([], ['"date:week'])
Model._read_group([], ['"create_date:week'])
with self.assertRaises(ValueError):
Model._read_group([], ['order_id.id'])
# Test malformed aggregate clause
with self.assertRaises(ValueError):
@@ -348,6 +351,9 @@ class TestPrivateReadGroup(common.TransactionCase):
with self.assertRaises(ValueError):
Model._read_group([], aggregates=['label:sum(value)'])
with self.assertWarns(Warning):
Model._read_group([], aggregates=['order_id.create_date:min'])
# Test malformed having clause
with self.assertRaises(ValueError):
Model._read_group([], ['value'], having=[('__count', '>')])
+2
View File
@@ -1954,6 +1954,8 @@ class BaseModel(metaclass=MetaModel):
field = self._fields[fname]
if func == 'recordset' and not (field.relational or fname == 'id'):
raise ValueError(f"Aggregate method {func!r} can be only used on relational field (or id) (for {aggregate_spec!r}).")
if property_name and field.type != 'property':
warnings.warn(f"Ignore the {property_name!r} part of {aggregate_spec!r}, this notation is reserved for the Property field")
sql_field = self._field_to_sql(self._table, access_fname, query)
sql_expr = READ_GROUP_AGGREGATE[func](self._table, sql_field)