From 7d5bef44400925e74bcbb584202eb2577509247e Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Sat, 12 Sep 2020 10:42:23 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20add=20support=20for=20emp?= =?UTF-8?q?tyArrayIfMissing=20on=20model=20attrs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/addon/models/target.js | 5 ++- addons/api/addon/serializers/application.js | 30 +++++++++++++++++- .../unit/serializers/application-test.js | 31 +++++++++++++++++++ .../api/tests/unit/serializers/target-test.js | 31 +++++++++++++++++++ ui/core/mirage/serializers/target.js | 12 ++++--- 5 files changed, 102 insertions(+), 7 deletions(-) diff --git a/addons/api/addon/models/target.js b/addons/api/addon/models/target.js index 90b3df16b..03a3ad5d7 100644 --- a/addons/api/addon/models/target.js +++ b/addons/api/addon/models/target.js @@ -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 diff --git a/addons/api/addon/serializers/application.js b/addons/api/addon/serializers/application.js index 65743c707..6dd0b84bd 100644 --- a/addons/api/addon/serializers/application.js +++ b/addons/api/addon/serializers/application.js @@ -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, diff --git a/addons/api/tests/unit/serializers/application-test.js b/addons/api/tests/unit/serializers/application-test.js index df6dca9db..f1962477b 100644 --- a/addons/api/tests/unit/serializers/application-test.js +++ b/addons/api/tests/unit/serializers/application-test.js @@ -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: {}, + }, + }); + }); }); diff --git a/addons/api/tests/unit/serializers/target-test.js b/addons/api/tests/unit/serializers/target-test.js index 62af72737..ab1a07635 100644 --- a/addons/api/tests/unit/serializers/target-test.js +++ b/addons/api/tests/unit/serializers/target-test.js @@ -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: {}, + }, + }); + }); + }); diff --git a/ui/core/mirage/serializers/target.js b/ui/core/mirage/serializers/target.js index 70850f457..2d2a0d974 100644 --- a/ui/core/mirage/serializers/target.js +++ b/ui/core/mirage/serializers/target.js @@ -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; }