diff --git a/addons/api/addon/generated/models/managed-group.js b/addons/api/addon/generated/models/managed-group.js index 279ee3898..d6502eee6 100644 --- a/addons/api/addon/generated/models/managed-group.js +++ b/addons/api/addon/generated/models/managed-group.js @@ -44,11 +44,4 @@ export default class GeneratedManagedGroupModel extends BaseModel { 'The ID of the Auth Method that is associated with this ManagedGroup', }) auth_method_id; - - @attr('array', { - description: - 'The IDs of the current set of members (accounts) that are associated with this ManagedGroup.', - readOnly: true, - }) - member_ids; } diff --git a/addons/api/addon/models/managed-group.js b/addons/api/addon/models/managed-group.js index 8a72a15b9..426807de2 100644 --- a/addons/api/addon/models/managed-group.js +++ b/addons/api/addon/models/managed-group.js @@ -1,12 +1,21 @@ import GeneratedManagedGroupModel from '../generated/models/managed-group'; import { attr } from '@ember-data/model'; import { fragment } from 'ember-data-model-fragments/attributes'; +import { A } from '@ember/array'; export default class ManagedGroupModel extends GeneratedManagedGroupModel { - // =error attributes - @attr('string', { readOnly: true }) filter; - // =attributes + /** + * Members is read-only under normal circumstances. But members can + * be persisted via calls to `addMembers()` or `removeMembers()`. + */ + @attr({ + readOnly: true, + defaultValue: () => A(), + emptyArrayIfMissing: true, + }) + member_ids; + @fragment('fragment-managed-group-attributes', { defaultValue: {} }) attributes; } diff --git a/addons/api/addon/serializers/fragment-managed-group-attributes.js b/addons/api/addon/serializers/fragment-managed-group-attributes.js index c250ec4df..230358a20 100644 --- a/addons/api/addon/serializers/fragment-managed-group-attributes.js +++ b/addons/api/addon/serializers/fragment-managed-group-attributes.js @@ -12,10 +12,16 @@ export default class FragmentManagedGroupAttributesSerializer extends JSONSerial * @param {Object} attribute */ serializeAttribute(snapshot, json, key, attribute) { - const { type } = attribute; + const { type, options } = attribute; let value = super.serializeAttribute(...arguments); // Convert empty string to null. if (type === 'string' && json[key] === '') json[key] = null; + // Do not serialize read-only attributes. + if (options.readOnly) delete json[key]; + // Version is sent only if it has a non-nullish value + if (key === 'version') { + if (json[key] === null || json[key] === undefined) delete json[key]; + } return value; } } diff --git a/addons/api/tests/unit/serializers/managed-group-test.js b/addons/api/tests/unit/serializers/managed-group-test.js index 561cddb61..4fb7e5a2b 100644 --- a/addons/api/tests/unit/serializers/managed-group-test.js +++ b/addons/api/tests/unit/serializers/managed-group-test.js @@ -60,6 +60,41 @@ module('Unit | Serializer | Managed group', function (hooks) { }); }); + test('it does not serialize version when it has null or undefined value', function (assert) { + assert.expect(1); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('managed-group'); + store.push({ + data: { + id: '1', + type: 'managed-group', + attributes: { + name: 'Group Test', + description: 'Description for the test', + member_ids: ['1', '2'], + version: undefined, + auth_method_id: '1234', + type: 'oidc', + attributes: { + filter: 'key=value', + }, + }, + }, + }); + const record = store.peekRecord('managed-group', '1'); + const snapshot = record._createSnapshot(); + const serializedRecord = serializer.serialize(snapshot); + assert.deepEqual(serializedRecord, { + name: 'Group Test', + description: 'Description for the test', + auth_method_id: '1234', + type: 'oidc', + attributes: { + filter: 'key=value', + }, + }); + }); + test('it does not serialize readOnly attributes', function (assert) { assert.expect(1); const store = this.owner.lookup('service:store');