From bbc3d46bd35dce10f186e81eabd88b21c2064afd Mon Sep 17 00:00:00 2001 From: pangratz Date: Tue, 8 Mar 2016 21:34:19 +0100 Subject: [PATCH] Strip code for DS_WARN_ON_UNKNOWN_KEYS warning in production - wrap code inside runInDebug so it is stripped from production - update checks so they work correctly with passed payload, which is actually a JSON-API document --- addon/-private/system/store.js | 39 ++++++++++++++++++++-------------- tests/unit/store/push-test.js | 30 +++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/addon/-private/system/store.js b/addon/-private/system/store.js index 09e95be4f52..b810eba43bb 100644 --- a/addon/-private/system/store.js +++ b/addon/-private/system/store.js @@ -4,7 +4,7 @@ import Ember from 'ember'; import Model from 'ember-data/model'; -import { assert, warn } from "ember-data/-private/debug"; +import { assert, warn, runInDebug } from "ember-data/-private/debug"; import _normalizeLink from "ember-data/-private/system/normalize-link"; import normalizeModelName from "ember-data/-private/system/normalize-model-name"; import { @@ -1674,21 +1674,28 @@ Store = Service.extend({ assert(`You must include an 'id' for ${modelName} in an object passed to 'push'`, data.id != null && data.id !== ''); assert(`You tried to push data with a type '${modelName}' but no model could be found with that name.`, this._hasModelFor(modelName)); - // If Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS is set to true and the payload - // contains unknown keys, log a warning. - - if (Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS) { - var type = this.modelFor(modelName); - warn("The payload for '" + type.modelName + "' contains these unknown keys: " + - Ember.inspect(Object.keys(data).forEach((key) => { - return !(key === 'id' || key === 'links' || get(type, 'fields').has(key) || key.match(/Type$/)); - })) + ". Make sure they've been defined in your model.", - Object.keys(data).filter((key) => { - return !(key === 'id' || key === 'links' || get(type, 'fields').has(key) || key.match(/Type$/)); - }).length === 0, - { id: 'ds.store.unknown-keys-in-payload' } - ); - } + runInDebug(() => { + // If Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS is set to true and the payload + // contains unknown attributes or relationships, log a warning. + + if (Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS) { + let type = this.modelFor(modelName); + + // Check unknown attributes + let unknownAttributes = Object.keys(data.attributes || {}).filter((key) => { + return !get(type, 'fields').has(key); + }); + let unknownAttributesMessage = `The payload for '${type.modelName}' contains these unknown attributes: ${unknownAttributes}. Make sure they've been defined in your model.`; + warn(unknownAttributesMessage, unknownAttributes.length === 0, { id: 'ds.store.unknown-keys-in-payload' }); + + // Check unknown relationships + let unknownRelationships = Object.keys(data.relationships || {}).filter((key) => { + return !get(type, 'fields').has(key); + }); + let unknownRelationshipsMessage = `The payload for '${type.modelName}' contains these unknown relationships: ${unknownRelationships}. Make sure they've been defined in your model.`; + warn(unknownRelationshipsMessage, unknownRelationships.length === 0, { id: 'ds.store.unknown-keys-in-payload' }); + } + }); // Actually load the record into the store. var internalModel = this._load(data); diff --git a/tests/unit/store/push-test.js b/tests/unit/store/push-test.js index aa5f2a036c3..bc29f255792 100644 --- a/tests/unit/store/push-test.js +++ b/tests/unit/store/push-test.js @@ -619,7 +619,7 @@ test('calling push with belongsTo relationship the value must not be an array', }, /must not be an array/); }); -test("Enabling Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS should warn on unknown keys", function(assert) { +test("Enabling Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS should warn on unknown attributes", function(assert) { run(function() { var originalFlagValue = Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS; try { @@ -636,7 +636,31 @@ test("Enabling Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS should warn on unknown keys", f } } }); - }); + }, "The payload for 'person' contains these unknown attributes: emailAddress,isMascot. Make sure they've been defined in your model."); + } finally { + Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS = originalFlagValue; + } + }); +}); + +test("Enabling Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS should warn on unknown relationships", function(assert) { + run(function() { + var originalFlagValue = Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS; + try { + Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS = true; + assert.expectWarning(function() { + store.push({ + data: { + type: 'person', + id: '1', + relationships: { + phoneNumbers: {}, + emailAddresses: {}, + mascots: {} + } + } + }); + }, "The payload for 'person' contains these unknown relationships: emailAddresses,mascots. Make sure they've been defined in your model."); } finally { Ember.ENV.DS_WARN_ON_UNKNOWN_KEYS = originalFlagValue; } @@ -658,7 +682,7 @@ test("Calling push with unknown keys should not warn by default", function(asser } }); }); - }, /The payload for 'person' contains these unknown keys: \[emailAddress,isMascot\]. Make sure they've been defined in your model./); + }, /The payload for 'person' contains these unknown .*: .* Make sure they've been defined in your model./); }); if (isEnabled('ds-pushpayload-return')) {