fix: 🐛 add support for emptyArrayIfMissing on model attrs
This commit is contained in:
5 files changed
+102
-7
No files matched your search
@@ -9,7 +9,10 @@ export default class TargetModel extends GeneratedTargetModel {
|
||||
/**
|
||||
* @type {[FragmentHostSetModel]}
|
||||
*/
|
||||
@fragmentArray('fragment-host-set', {readOnly: true}) host_sets;
|
||||
@fragmentArray('fragment-host-set', {
|
||||
readOnly: true,
|
||||
emptyArrayIfMissing: true
|
||||
}) host_sets;
|
||||
|
||||
/**
|
||||
* An array of resolved host set and host catalog instances. Model instances
|
||||
|
||||
@@ -117,6 +117,7 @@ export default class ApplicationSerializer extends RESTSerializer {
|
||||
* This method makes this transformation to accommodate Ember Data.
|
||||
*
|
||||
* @method normalizeSingleResponse
|
||||
* @override
|
||||
* @param {Store} store
|
||||
* @param {Model} primaryModelClass
|
||||
* @param {Object} payload
|
||||
@@ -125,6 +126,10 @@ export default class ApplicationSerializer extends RESTSerializer {
|
||||
* @return {Object} JSON-API Document
|
||||
*/
|
||||
normalizeSingleResponse(store, primaryModelClass, payload, id, requestType) {
|
||||
// Copy payload (mostly to prevent mocking issues)
|
||||
payload = copy(payload, true);
|
||||
// Check for and normalize missing arrays
|
||||
payload = this.normalizeMissingArrays(store, primaryModelClass, payload);
|
||||
// Setup a new payload data structure.
|
||||
const transformedPayload = {};
|
||||
// Find the Ember-data-expected root key name.
|
||||
@@ -132,7 +137,7 @@ export default class ApplicationSerializer extends RESTSerializer {
|
||||
primaryModelClass.modelName
|
||||
);
|
||||
// Copy the unrooted payload under the expected root key name.
|
||||
transformedPayload[payloadKey] = copy(payload, true);
|
||||
transformedPayload[payloadKey] = payload;
|
||||
// Return the result of normalizing the transformed payload.
|
||||
return super.normalizeSingleResponse(
|
||||
store,
|
||||
@@ -143,6 +148,29 @@ export default class ApplicationSerializer extends RESTSerializer {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets missing array fields to an empty array if they are annotated by
|
||||
* `normalizeToEmptyArray: true` in the associated model attribute
|
||||
* declaration. Our API excludes arrays when they are empty from
|
||||
* singular responses.
|
||||
*
|
||||
* @param {Store} store
|
||||
* @param {Model} primaryModelClass
|
||||
* @param {object} payload
|
||||
* @return {object}
|
||||
*/
|
||||
normalizeMissingArrays(store, primaryModelClass, payload) {
|
||||
const attrDefs = store._attributesDefinitionFor(primaryModelClass.modelName);
|
||||
if (attrDefs) {
|
||||
Object.keys(attrDefs).forEach(key => {
|
||||
if (!payload[key] && attrDefs[key]?.options?.emptyArrayIfMissing) {
|
||||
payload[key] = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
* One consequence of using model fragments to represent the embedded scope
|
||||
* is that they cannot have `id` fields. We still need to know scope IDs,
|
||||
|
||||
@@ -138,4 +138,35 @@ module('Unit | Serializer | application', function (hooks) {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('it normalizes missing arrays in single responses if annotated in model attributes', function (assert) {
|
||||
assert.expect(2);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('target');
|
||||
const target = store.createRecord('target').constructor;
|
||||
const payload = {
|
||||
id: '1',
|
||||
name: 'Target 1',
|
||||
scope: { id: 'o_123' }
|
||||
};
|
||||
const normalized = serializer.normalizeSingleResponse(
|
||||
store,
|
||||
target,
|
||||
payload
|
||||
);
|
||||
assert.notOk(payload.bogusArrayField);
|
||||
assert.deepEqual(normalized, {
|
||||
included: [],
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'target',
|
||||
attributes: {
|
||||
name: 'Target 1',
|
||||
scope: { scope_id: 'o_123' },
|
||||
host_sets: []
|
||||
},
|
||||
relationships: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -94,4 +94,35 @@ module('Unit | Serializer | target', function(hooks) {
|
||||
});
|
||||
});
|
||||
|
||||
test('it normalizes missing host_sets to empty array', function (assert) {
|
||||
assert.expect(2);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('target');
|
||||
const target = store.createRecord('target').constructor;
|
||||
const payload = {
|
||||
id: '1',
|
||||
name: 'Target 1',
|
||||
scope: { id: 'o_123' }
|
||||
};
|
||||
const normalized = serializer.normalizeSingleResponse(
|
||||
store,
|
||||
target,
|
||||
payload
|
||||
);
|
||||
assert.notOk(payload.bogusArrayField);
|
||||
assert.deepEqual(normalized, {
|
||||
included: [],
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'target',
|
||||
attributes: {
|
||||
name: 'Target 1',
|
||||
scope: { scope_id: 'o_123' },
|
||||
host_sets: []
|
||||
},
|
||||
relationships: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -13,11 +13,13 @@ export default ApplicationSerializer.extend({
|
||||
_hashForModel(model) {
|
||||
const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments);
|
||||
const { hostSets } = this.schema;
|
||||
json.host_sets = model.hostSetIds.map(host_set_id => {
|
||||
const hostSet = hostSets.find(host_set_id);
|
||||
const host_catalog_id = hostSet?.hostCatalog?.id;
|
||||
return { id: host_set_id, host_catalog_id };
|
||||
});
|
||||
if (model.hostSetIds?.length) {
|
||||
json.host_sets = model.hostSetIds.map(host_set_id => {
|
||||
const hostSet = hostSets.find(host_set_id);
|
||||
const host_catalog_id = hostSet?.hostCatalog?.id;
|
||||
return { id: host_set_id, host_catalog_id };
|
||||
});
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user