fix: 🐛 handle empty member_ids in group responses
This commit is contained in:
3 files changed
+36
-2
No files matched your search
@@ -11,7 +11,11 @@ export default class GroupModel extends GeneratedGroupModel {
|
|||||||
* Members is read-only under normal circumstances. But members can
|
* Members is read-only under normal circumstances. But members can
|
||||||
* be persisted via calls to `addMembers()` or `removeMembers()`.
|
* be persisted via calls to `addMembers()` or `removeMembers()`.
|
||||||
*/
|
*/
|
||||||
@attr({readOnly: true, defaultValue: () => A()}) member_ids;
|
@attr({
|
||||||
|
readOnly: true,
|
||||||
|
defaultValue: () => A(),
|
||||||
|
emptyArrayIfMissing: true
|
||||||
|
}) member_ids;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An array of resolved user instances.
|
* An array of resolved user instances.
|
||||||
|
|||||||
@@ -45,4 +45,34 @@ module('Unit | Serializer | group', function(hooks) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('it normalizes missing member_ids to empty array', function (assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
const store = this.owner.lookup('service:store');
|
||||||
|
const serializer = store.serializerFor('group');
|
||||||
|
const group = store.createRecord('group').constructor;
|
||||||
|
const payload = {
|
||||||
|
id: '1',
|
||||||
|
name: 'Group 1',
|
||||||
|
scope: { id: 'o_123' }
|
||||||
|
};
|
||||||
|
const normalized = serializer.normalizeSingleResponse(
|
||||||
|
store,
|
||||||
|
group,
|
||||||
|
payload
|
||||||
|
);
|
||||||
|
assert.deepEqual(normalized, {
|
||||||
|
included: [],
|
||||||
|
data: {
|
||||||
|
id: '1',
|
||||||
|
type: 'group',
|
||||||
|
attributes: {
|
||||||
|
name: 'Group 1',
|
||||||
|
scope: { scope_id: 'o_123' },
|
||||||
|
member_ids: []
|
||||||
|
},
|
||||||
|
relationships: {},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -12,7 +12,7 @@ export default ApplicationSerializer.extend({
|
|||||||
|
|
||||||
_hashForModel(model) {
|
_hashForModel(model) {
|
||||||
const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments);
|
const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments);
|
||||||
json.member_ids = model.memberIds;
|
if (model.memberIds.length) json.member_ids = model.memberIds;
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user