fix: 🐛 handle missing host_ids array in host sets
This commit is contained in:
3 files changed
+39
-2
No files matched your search
@@ -9,7 +9,10 @@ export default class HostSetModel extends GeneratedHostSetModel {
|
||||
* Grants is read-only _most_ under normal circumstances. But grants can
|
||||
* be persisted via a dedicated call to `saveGrants()`.
|
||||
*/
|
||||
@fragmentArray('fragment-string', {readOnly: true}) host_ids;
|
||||
@fragmentArray('fragment-string', {
|
||||
readOnly: true,
|
||||
emptyArrayIfMissing: true
|
||||
}) host_ids;
|
||||
|
||||
// =methods
|
||||
|
||||
|
||||
@@ -74,4 +74,36 @@ module('Unit | Serializer | host set', function(hooks) {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('it normalizes missing host_ids to empty array', function (assert) {
|
||||
assert.expect(2);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('host-set');
|
||||
const hostSet = store.createRecord('host-set').constructor;
|
||||
const payload = {
|
||||
id: '1',
|
||||
name: 'Host Set 1',
|
||||
scope: { id: 'o_123' }
|
||||
};
|
||||
const normalized = serializer.normalizeSingleResponse(
|
||||
store,
|
||||
hostSet,
|
||||
payload
|
||||
);
|
||||
assert.notOk(payload.bogusArrayField);
|
||||
assert.deepEqual(normalized, {
|
||||
included: [],
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'host-set',
|
||||
attributes: {
|
||||
name: 'Host Set 1',
|
||||
scope: { scope_id: 'o_123' },
|
||||
host_ids: []
|
||||
},
|
||||
relationships: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -12,8 +12,10 @@ export default ApplicationSerializer.extend({
|
||||
|
||||
_hashForModel(model) {
|
||||
const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments);
|
||||
json.host_ids = model.hostIds;
|
||||
json.host_catalog_id = model.hostCatalogId;
|
||||
if (model.hostIds?.length) {
|
||||
json.host_ids = model.hostIds;
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user