Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip code for DS_WARN_ON_UNKNOWN_KEYS warning in production #4222

Merged
merged 1 commit into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact: All the code inside runInDebug get striped but this comment is preserved.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😜


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);
Expand Down
30 changes: 27 additions & 3 deletions tests/unit/store/push-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand All @@ -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')) {
Expand Down