diff --git a/FEATURES.md b/FEATURES.md index 5fa0f1d1fcc..721b054ba9d 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -133,3 +133,7 @@ entry in `config/features.json`. } ``` +- `ds-check-should-serialize-relationships` + +Adds public method for `shouldSerializeHasMany`, used to determine if a +`hasMany` relationship can be serialized. diff --git a/addon/serializers/json-api.js b/addon/serializers/json-api.js index 10253f42a61..df31f0adec6 100644 --- a/addon/serializers/json-api.js +++ b/addon/serializers/json-api.js @@ -571,8 +571,12 @@ const JSONAPISerializer = JSONSerializer.extend({ */ serializeHasMany(snapshot, json, relationship) { var key = relationship.key; + var shouldSerializeHasMany = '_shouldSerializeHasMany'; + if (isEnabled("ds-check-should-serialize-relationships")) { + shouldSerializeHasMany = 'shouldSerializeHasMany'; + } - if (this._shouldSerializeHasMany(snapshot, key, relationship)) { + if (this[shouldSerializeHasMany](snapshot, key, relationship)) { var hasMany = snapshot.hasMany(key); if (hasMany !== undefined) { diff --git a/addon/serializers/json.js b/addon/serializers/json.js index ef4ca3a9bbc..0833e0bc1ae 100644 --- a/addon/serializers/json.js +++ b/addon/serializers/json.js @@ -840,6 +840,27 @@ var JSONSerializer = Serializer.extend({ return attrs && attrs[key] && attrs[key].serialize === true; }, + /** + Check if the given hasMany relationship should be serialized + + @method shouldSerializeHasMany + @param {DS.Snapshot} snapshot + @param {String} key + @param {String} relationshipType + @return {boolean} true if the hasMany relationship should be serialized + */ + + shouldSerializeHasMany(snapshot, key, relationship) { + if ((this._shouldSerializeHasMany !== JSONSerializer.prototype._shouldSerializeHasMany)) { + deprecate('The private method _shouldSerializeHasMany has been promoted to the public API. Please remove the underscore to use the public shouldSerializeHasMany method.', false, { + id: 'ds.serializer.private-should-serialize-has-many', + until: '3.0.0' + }); + } + + return this._shouldSerializeHasMany(snapshot, key, relationship); + }, + /** Check if the given hasMany relationship should be serialized @@ -1196,8 +1217,12 @@ var JSONSerializer = Serializer.extend({ */ serializeHasMany(snapshot, json, relationship) { var key = relationship.key; + var shouldSerializeHasMany = '_shouldSerializeHasMany'; + if (isEnabled("ds-check-should-serialize-relationships")) { + shouldSerializeHasMany = 'shouldSerializeHasMany'; + } - if (this._shouldSerializeHasMany(snapshot, key, relationship)) { + if (this[shouldSerializeHasMany](snapshot, key, relationship)) { var hasMany = snapshot.hasMany(key, { ids: true }); if (hasMany !== undefined) { // if provided, use the mapping provided by `attrs` in diff --git a/config/features.json b/config/features.json index 786c106b184..0e486cb5006 100644 --- a/config/features.json +++ b/config/features.json @@ -6,5 +6,6 @@ "ds-extended-errors": null, "ds-links-in-record-array": null, "ds-overhaul-references": null, - "ds-payload-type-hooks": null + "ds-payload-type-hooks": null, + "ds-check-should-serialize-relationships": null } diff --git a/tests/integration/serializers/json-serializer-test.js b/tests/integration/serializers/json-serializer-test.js index 773feab2246..73fc5b9bf50 100644 --- a/tests/integration/serializers/json-serializer-test.js +++ b/tests/integration/serializers/json-serializer-test.js @@ -201,6 +201,32 @@ test("serializeHasMany omits unknown relationships on pushed record", function(a assert.ok(!json.hasOwnProperty("comments"), "Does not add the relationship key to json"); }); +test("shouldSerializeHasMany", function(assert) { + + run(function() { + post = env.store.createRecord('post', { title: "Rails is omakase", id: "1" }); + comment = env.store.createRecord('comment', { body: "Omakase is delicious", post: post, id: "1" }); + }); + + var snapshot = post._createSnapshot(); + var relationship = snapshot.record.relationshipFor('comments'); + var key = relationship.key; + + var shouldSerialize = env.store.serializerFor("post").shouldSerializeHasMany(snapshot, relationship, key); + + assert.ok(shouldSerialize, 'shouldSerializeHasMany correctly identifies with hasMany relationship'); +}); + +if (isEnabled("ds-check-should-serialize-relationships")) { + testInDebug("_shouldSerializeHasMany deprecation", function(assert) { + env.store.serializerFor("post")._shouldSerializeHasMany = Ember.K; + + assert.expectDeprecation(function() { + env.store.serializerFor("post").shouldSerializeHasMany(); + }, /_shouldSerializeHasMany has been promoted to the public API/); + }); +} + test("serializeIntoHash", function(assert) { run(function() { post = env.store.createRecord('post', { title: "Rails is omakase" });