feat: 🎸 Refactor serializer and model for managed-group
Refactor serializer and model with suggestions from previous PR for managed-group.
This commit is contained in:
1 parent
14623a6386
commit
087e1346d3
4 files changed
+54
-11
No files matched your search
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
|
||||
Reference in new issue
Block a user