0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00

Added property cleaning to tag relations on pages + posts

refs https://github.com/TryGhost/Ghost/pull/15375

- we currently pass all properties for the `tags` property of a
  `page`/`post` body down further into Ghost, which is causing issues
  because it's handling properties it doesn't expect
- this is showing up because it's triggering save history events for
  tags when a post is edited
- this commit introduces a clean util which has an allowlist of
  properties allows on tag relations
- this list was taken from the schema: 128f8fb006/packages/admin-api-schema/lib/schemas/posts.json (L214-L227)
This commit is contained in:
Daniel Lockyer 2022-09-06 21:47:52 +01:00 committed by Daniel Lockyer
parent 290a5f2ceb
commit 04e3ee9f10
5 changed files with 56 additions and 0 deletions

View file

@ -5,6 +5,7 @@ const url = require('./utils/url');
const slugFilterOrder = require('./utils/slug-filter-order'); const slugFilterOrder = require('./utils/slug-filter-order');
const localUtils = require('../../index'); const localUtils = require('../../index');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta; const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;
const clean = require('./utils/clean');
function removeMobiledocFormat(frame) { function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) { if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
@ -159,6 +160,8 @@ module.exports = {
frame.data.pages[0].tags[index] = { frame.data.pages[0].tags[index] = {
name: tag name: tag
}; };
} else {
frame.data.pages[0].tags[index] = clean.pagesTag(tag);
} }
}); });
} }

View file

@ -5,6 +5,7 @@ const slugFilterOrder = require('./utils/slug-filter-order');
const localUtils = require('../../index'); const localUtils = require('../../index');
const mobiledoc = require('../../../../../lib/mobiledoc'); const mobiledoc = require('../../../../../lib/mobiledoc');
const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta; const postsMetaSchema = require('../../../../../data/schema').tables.posts_meta;
const clean = require('./utils/clean');
function removeMobiledocFormat(frame) { function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) { if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
@ -175,6 +176,8 @@ module.exports = {
frame.data.posts[0].tags[index] = { frame.data.posts[0].tags[index] = {
name: tag name: tag
}; };
} else {
frame.data.posts[0].tags[index] = clean.postsTag(tag);
} }
}); });
} }

View file

@ -0,0 +1,12 @@
const _ = require('lodash');
const tagRelation = (attrs) => {
return _.pick(attrs, [
'id',
'name',
'slug'
]);
};
module.exports.pagesTag = tagRelation;
module.exports.postsTag = tagRelation;

View file

@ -158,6 +158,25 @@ describe('Unit: endpoints/utils/serializers/input/pages', function () {
}); });
}); });
it('tags relation is stripped of unknown properties', function () {
const apiConfig = {};
const frame = {
options: {},
data: {
pages: [
{
id: 'id1',
tags: [{slug: 'slug1', name: 'hey', parent: null}, {slug: 'slug2'}]
}
]
}
};
serializers.input.pages.edit(apiConfig, frame);
frame.data.pages[0].tags.should.eql([{slug: 'slug1', name: 'hey'}, {slug: 'slug2'}]);
});
describe('Ensure relations format', function () { describe('Ensure relations format', function () {
it('relations is array of objects', function () { it('relations is array of objects', function () {
const apiConfig = {}; const apiConfig = {};

View file

@ -274,6 +274,25 @@ describe('Unit: endpoints/utils/serializers/input/posts', function () {
}); });
}); });
it('tags relation is stripped of unknown properties', function () {
const apiConfig = {};
const frame = {
options: {},
data: {
posts: [
{
id: 'id1',
tags: [{slug: 'slug1', name: 'hey', parent: null}, {slug: 'slug2'}]
}
]
}
};
serializers.input.posts.edit(apiConfig, frame);
frame.data.posts[0].tags.should.eql([{slug: 'slug1', name: 'hey'}, {slug: 'slug2'}]);
});
describe('Ensure relations format', function () { describe('Ensure relations format', function () {
it('relations is array of objects', function () { it('relations is array of objects', function () {
const apiConfig = {}; const apiConfig = {};