[FIX] web: default_get returning unknown field

Before this rev., it crashed when trying to create records of the
res.users model. This is due to commit ad1cceb.

The 'default_get' of 'res.users' always returns a value for field
'groups_id', whereas the docstring of 'default_get' states that it
only returns default values for fields given in arguments.

Rev. ad1cceb changed a bit the way the result of 'default_get' was
processed, by iterating over the values instead of over the fields
in the view. So, before ad1cceb, 'groups_id' was simply ignored,
and after, the BasicModel tried to process it and it crashed when it
attempted to retrieve its fieldsInfo, as this field isn't in the
view.

This rev. restores the previous behavior.

Fixes #23303
Fixes #23305
This commit is contained in:
Aaron Bohy
2018-02-26 12:45:10 +01:00
parent 5450c5c3f7
commit 034c075e7e
2 changed files with 40 additions and 3 deletions
@@ -219,6 +219,10 @@ var BasicModel = AbstractModel.extend({
var fieldName;
record._changes = record._changes || {};
// ignore values for non requested fields (for instance, fields that are
// not in the view)
values = _.pick(values, fieldNames);
// fill default values for missing fields
for (var i = 0; i < fieldNames.length; i++) {
fieldName = fieldNames[i];
@@ -240,9 +244,6 @@ var BasicModel = AbstractModel.extend({
var defs = [];
for (fieldName in values) {
field = record.fields[fieldName];
if (!field) {
continue; // ignore values for unknown fields
}
record.data[fieldName] = null;
var dp;
if (field.type === 'many2one' && values[fieldName]) {
@@ -669,6 +669,42 @@ QUnit.module('Views', {
model.destroy();
});
QUnit.test('default_get returning a non requested field', function (assert) {
// 'default_get' returns a default value for the fields given in
// arguments. It should not return a value for fields that have not be
// requested. However, it happens (e.g. res.users), and the webclient
// should not crash when this situation occurs (the field should simply
// be ignored).
assert.expect(2);
this.params.context = {};
this.params.fieldNames = ['category'];
this.params.res_id = undefined;
this.params.type = 'record';
var model = createModel({
Model: BasicModel,
data: this.data,
mockRPC: function (route, args) {
var result = this._super(route, args);
if (args.method === 'default_get') {
result.product_ids = [[6, 0, [37, 41]]];
}
return result;
},
});
model.load(this.params).then(function (resultID) {
var record = model.get(resultID);
assert.ok('category' in record.data,
"should have processed 'category'");
assert.notOk('product_ids' in record.data,
"should have ignored 'product_ids'");
});
model.destroy();
});
QUnit.test('can make a default_record with default relational values', function (assert) {
assert.expect(7);