fix: 🐛 handle missing principals array in role response

This commit is contained in:
Randall Morey committed 2020-09-12 11:40:21 -04:00
1 parent 048e202343
commit bcd46cec1f
6 files changed
+39 -7

No files matched your search

+4 -1
View File
@@ -12,7 +12,10 @@ export default class RoleModel extends GeneratedRoleModel {
* see obvious. Instead, the application layer is expected to load referenced
* users and groups as needed.
*/
@fragmentArray('fragment-principal', {readOnly: true}) principals;
@fragmentArray('fragment-principal', {
readOnly: true,
emptyArrayIfMissing: true
}) principals;
/**
* Grants is read-only _most_ under normal circumstances. But grants can
@@ -140,7 +140,7 @@ module('Unit | Serializer | application', function (hooks) {
});
test('it normalizes missing arrays in single responses if annotated in model attributes', function (assert) {
assert.expect(2);
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
const target = store.createRecord('target').constructor;
@@ -154,7 +154,6 @@ module('Unit | Serializer | application', function (hooks) {
target,
payload
);
assert.notOk(payload.bogusArrayField);
assert.deepEqual(normalized, {
included: [],
data: {
@@ -76,7 +76,7 @@ module('Unit | Serializer | host set', function(hooks) {
});
test('it normalizes missing host_ids to empty array', function (assert) {
assert.expect(2);
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('host-set');
const hostSet = store.createRecord('host-set').constructor;
@@ -90,7 +90,6 @@ module('Unit | Serializer | host set', function(hooks) {
hostSet,
payload
);
assert.notOk(payload.bogusArrayField);
assert.deepEqual(normalized, {
included: [],
data: {
@@ -68,6 +68,37 @@ module('Unit | Serializer | role', function(hooks) {
attributes: {
name: 'Role 1',
grants: [{value: '*'}, {value: '*'}],
principals: []
},
relationships: {},
},
});
});
test('it normalizes missing principals to empty array', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('role');
const role = store.createRecord('role').constructor;
const payload = {
id: '1',
name: 'Role 1',
scope: { id: 'o_123' }
};
const normalized = serializer.normalizeSingleResponse(
store,
role,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'role',
attributes: {
name: 'Role 1',
scope: { scope_id: 'o_123' },
principals: []
},
relationships: {},
},
@@ -95,7 +95,7 @@ module('Unit | Serializer | target', function(hooks) {
});
test('it normalizes missing host_sets to empty array', function (assert) {
assert.expect(2);
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
const target = store.createRecord('target').constructor;
@@ -109,7 +109,6 @@ module('Unit | Serializer | target', function(hooks) {
target,
payload
);
assert.notOk(payload.bogusArrayField);
assert.deepEqual(normalized, {
included: [],
data: {
+1
View File
@@ -16,6 +16,7 @@ export default ApplicationSerializer.extend({
type: 'group'
}));
json.principals = [ ...serializedUsers, ...serializedGroups ];
if (!json.principals.length) delete json.principals;
return json;
}
});