0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Filter options passed to toJSON

fixes #5177

- we now pass API/model options directly to toJSON, which is unsafe as these options haven't always been filtered before they are passed.
- this fix adds a filter so that toJSON only uses the options it needs
- additionally, rename the 'name' option to something more specific to prevent clashes
This commit is contained in:
Hannah Wolfe 2015-04-22 20:20:27 +01:00
parent d480ee4912
commit 32125c2f46

View file

@ -138,6 +138,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
var attrs = _.extend({}, this.attributes), var attrs = _.extend({}, this.attributes),
self = this; self = this;
options = options || {}; options = options || {};
options = _.pick(options, ['shallow', 'baseKey', 'include', 'context']);
if (options && options.shallow) { if (options && options.shallow) {
return attrs; return attrs;
@ -150,9 +151,9 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
_.each(this.relations, function (relation, key) { _.each(this.relations, function (relation, key) {
if (key.substring(0, 7) !== '_pivot_') { if (key.substring(0, 7) !== '_pivot_') {
// if include is set, expand to full object // if include is set, expand to full object
var fullKey = _.isEmpty(options.name) ? key : options.name + '.' + key; var fullKey = _.isEmpty(options.baseKey) ? key : options.baseKey + '.' + key;
if (_.contains(self.include, fullKey)) { if (_.contains(self.include, fullKey)) {
attrs[key] = relation.toJSON(_.extend({}, options, {name: fullKey, include: self.include})); attrs[key] = relation.toJSON(_.extend({}, options, {baseKey: fullKey, include: self.include}));
} }
} }
}); });