From b5155e280e267d31862fafd3947b6152a76c9e8f Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Mon, 25 Feb 2019 11:08:28 +0100 Subject: [PATCH] Returned tags & authors by default for Admin API v2 no issue - tags, authors, authors.roles by default --- .../api/v2/utils/serializers/input/pages.js | 16 ++++++++ .../api/v2/utils/serializers/input/posts.js | 16 ++++++++ core/test/acceptance/old/admin/pages_spec.js | 6 +-- core/test/acceptance/old/admin/posts_spec.js | 39 ++++++++----------- core/test/acceptance/old/admin/utils.js | 4 ++ core/test/regression/api/v2/admin/utils.js | 6 ++- 6 files changed, 60 insertions(+), 27 deletions(-) diff --git a/core/server/api/v2/utils/serializers/input/pages.js b/core/server/api/v2/utils/serializers/input/pages.js index 02f2e475a2..b8587bb9f4 100644 --- a/core/server/api/v2/utils/serializers/input/pages.js +++ b/core/server/api/v2/utils/serializers/input/pages.js @@ -12,6 +12,18 @@ function removeMobiledocFormat(frame) { } } +function defaultRelations(frame) { + if (frame.options.withRelated) { + return; + } + + if (frame.options.columns && !frame.options.withRelated) { + return false; + } + + frame.options.withRelated = ['tags', 'authors', 'authors.roles']; +} + function setDefaultOrder(frame) { let includesOrderedRelations = false; @@ -71,6 +83,7 @@ module.exports = { if (!localUtils.isContentAPI(frame)) { forceStatusFilter(frame); defaultFormat(frame); + defaultRelations(frame); } debug(frame.options); @@ -89,6 +102,7 @@ module.exports = { if (!localUtils.isContentAPI(frame)) { forceStatusFilter(frame); defaultFormat(frame); + defaultRelations(frame); } debug(frame.options); @@ -134,6 +148,7 @@ module.exports = { } defaultFormat(frame); + defaultRelations(frame); }, edit(apiConfig, frame) { @@ -152,5 +167,6 @@ module.exports = { }; defaultFormat(frame); + defaultRelations(frame); } }; diff --git a/core/server/api/v2/utils/serializers/input/posts.js b/core/server/api/v2/utils/serializers/input/posts.js index 58de3fc7e5..2a5271d688 100644 --- a/core/server/api/v2/utils/serializers/input/posts.js +++ b/core/server/api/v2/utils/serializers/input/posts.js @@ -21,6 +21,18 @@ function includeTags(frame) { } } +function defaultRelations(frame) { + if (frame.options.withRelated) { + return; + } + + if (frame.options.columns && !frame.options.withRelated) { + return false; + } + + frame.options.withRelated = ['tags', 'authors', 'authors.roles']; +} + function setDefaultOrder(frame) { let includesOrderedRelations = false; @@ -93,6 +105,7 @@ module.exports = { if (!localUtils.isContentAPI(frame)) { forceStatusFilter(frame); defaultFormat(frame); + defaultRelations(frame); } debug(frame.options); @@ -124,6 +137,7 @@ module.exports = { if (!localUtils.isContentAPI(frame)) { forceStatusFilter(frame); defaultFormat(frame); + defaultRelations(frame); } debug(frame.options); @@ -169,6 +183,7 @@ module.exports = { } defaultFormat(frame); + defaultRelations(frame); }, edit(apiConfig, frame) { @@ -185,5 +200,6 @@ module.exports = { }; defaultFormat(frame); + defaultRelations(frame); } }; diff --git a/core/test/acceptance/old/admin/pages_spec.js b/core/test/acceptance/old/admin/pages_spec.js index 604a7c9c77..cce37025b6 100644 --- a/core/test/acceptance/old/admin/pages_spec.js +++ b/core/test/acceptance/old/admin/pages_spec.js @@ -45,7 +45,7 @@ describe('Pages API', function () { localUtils.API.checkResponse(jsonResponse, 'pages'); jsonResponse.pages.should.have.length(2); - localUtils.API.checkResponse(jsonResponse.pages[0], 'page', null, ['primary_tag', 'primary_author']); + localUtils.API.checkResponse(jsonResponse.pages[0], 'page'); localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination'); _.isBoolean(jsonResponse.pages[0].featured).should.eql(true); @@ -75,7 +75,7 @@ describe('Pages API', function () { // @NOTE: we do not return "excerpt" if custom_excerpt or plaintext is empty // @NOTE: we do not return primary_* if null - localUtils.API.checkResponse(res.body.pages[0], 'page', null, ['primary_tag', 'primary_author', 'excerpt']); + localUtils.API.checkResponse(res.body.pages[0], 'page', null, ['excerpt']); should.exist(res.headers['x-cache-invalidate']); return models.Post.findOne({ @@ -111,7 +111,7 @@ describe('Pages API', function () { }) .then((res) => { should.exist(res.headers['x-cache-invalidate']); - localUtils.API.checkResponse(res.body.pages[0], 'page', null, ['primary_tag', 'primary_author']); + localUtils.API.checkResponse(res.body.pages[0], 'page'); return models.Post.findOne({ id: res.body.pages[0].id diff --git a/core/test/acceptance/old/admin/posts_spec.js b/core/test/acceptance/old/admin/posts_spec.js index 4ae9b6222c..47b7d3eaa8 100644 --- a/core/test/acceptance/old/admin/posts_spec.js +++ b/core/test/acceptance/old/admin/posts_spec.js @@ -44,7 +44,7 @@ describe('Posts API', function () { should.exist(jsonResponse.posts); localUtils.API.checkResponse(jsonResponse, 'posts'); jsonResponse.posts.should.have.length(13); - localUtils.API.checkResponse(jsonResponse.posts[0], 'post', null, ['primary_tag', 'primary_author']); + localUtils.API.checkResponse(jsonResponse.posts[0], 'post'); localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination'); _.isBoolean(jsonResponse.posts[0].featured).should.eql(true); @@ -57,6 +57,12 @@ describe('Posts API', function () { jsonResponse.posts[2].url.should.eql(`${config.get('url')}/welcome/`); jsonResponse.posts[11].feature_image.should.eql(`${config.get('url')}/content/images/2018/hey.jpg`); + jsonResponse.posts[0].tags.length.should.eql(0); + jsonResponse.posts[2].tags.length.should.eql(1); + jsonResponse.posts[2].authors.length.should.eql(1); + jsonResponse.posts[2].tags[0].url.should.eql(`${config.get('url')}/tag/getting-started/`); + jsonResponse.posts[2].authors[0].url.should.eql(`${config.get('url')}/author/ghost/`); + done(); }); }); @@ -77,7 +83,7 @@ describe('Posts API', function () { should.exist(jsonResponse.posts); localUtils.API.checkResponse(jsonResponse, 'posts'); jsonResponse.posts.should.have.length(3); - localUtils.API.checkResponse(jsonResponse.posts[0], 'post', ['mobiledoc', 'plaintext'], ['primary_tag', 'primary_author']); + localUtils.API.checkResponse(jsonResponse.posts[0], 'post', ['plaintext']); localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination'); _.isBoolean(jsonResponse.posts[0].featured).should.eql(true); @@ -88,8 +94,8 @@ describe('Posts API', function () { }); }); - it('Can includes relations', function (done) { - request.get(localUtils.API.getApiQuery('posts/?include=tags,authors')) + it('Can includes single relation', function (done) { + request.get(localUtils.API.getApiQuery('posts/?include=tags')) .set('Origin', config.get('url')) .expect('Content-Type', /json/) .expect('Cache-Control', testUtils.cacheRules.private) @@ -107,17 +113,11 @@ describe('Posts API', function () { localUtils.API.checkResponse( jsonResponse.posts[0], 'post', - ['tags', 'authors'] + null, + ['authors', 'primary_author'] ); localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination'); - - jsonResponse.posts[0].tags.length.should.eql(0); - jsonResponse.posts[2].tags.length.should.eql(1); - jsonResponse.posts[2].authors.length.should.eql(1); - jsonResponse.posts[2].tags[0].url.should.eql(`${config.get('url')}/tag/getting-started/`); - jsonResponse.posts[2].authors[0].url.should.eql(`${config.get('url')}/author/ghost/`); - done(); }); }); @@ -138,7 +138,7 @@ describe('Posts API', function () { should.exist(jsonResponse.posts); localUtils.API.checkResponse(jsonResponse, 'posts'); jsonResponse.posts.should.have.length(2); - localUtils.API.checkResponse(jsonResponse.posts[0], 'post', null, ['primary_tag', 'primary_author']); + localUtils.API.checkResponse(jsonResponse.posts[0], 'post'); localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination'); done(); }); @@ -196,14 +196,12 @@ describe('Posts API', function () { var jsonResponse = res.body; should.exist(jsonResponse); should.exist(jsonResponse.posts); - localUtils.API.checkResponse(jsonResponse.posts[0], 'post', null, ['primary_tag', 'primary_author']); + localUtils.API.checkResponse(jsonResponse.posts[0], 'post'); jsonResponse.posts[0].id.should.equal(testUtils.DataGenerator.Content.posts[0].id); _.isBoolean(jsonResponse.posts[0].featured).should.eql(true); testUtils.API.isISO8601(jsonResponse.posts[0].created_at).should.be.true(); - // Tags aren't included by default - should.not.exist(jsonResponse.posts[0].tags); done(); }); }); @@ -223,13 +221,10 @@ describe('Posts API', function () { var jsonResponse = res.body; should.exist(jsonResponse); should.exist(jsonResponse.posts); - localUtils.API.checkResponse(jsonResponse.posts[0], 'post', null, ['primary_tag', 'primary_author']); + localUtils.API.checkResponse(jsonResponse.posts[0], 'post'); jsonResponse.posts[0].slug.should.equal('welcome'); _.isBoolean(jsonResponse.posts[0].featured).should.eql(true); - - // Tags aren't included by default - should.not.exist(jsonResponse.posts[0].tags); done(); }); }); @@ -251,7 +246,7 @@ describe('Posts API', function () { should.exist(jsonResponse); should.exist(jsonResponse.posts); - localUtils.API.checkResponse(jsonResponse.posts[0], 'post', ['tags', 'authors']); + localUtils.API.checkResponse(jsonResponse.posts[0], 'post'); jsonResponse.posts[0].authors[0].should.be.an.Object(); localUtils.API.checkResponse(jsonResponse.posts[0].authors[0], 'user', ['url']); @@ -282,7 +277,7 @@ describe('Posts API', function () { .expect(201) .then((res) => { res.body.posts.length.should.eql(1); - localUtils.API.checkResponse(res.body.posts[0], 'post', null, ['primary_tag', 'primary_author']); + localUtils.API.checkResponse(res.body.posts[0], 'post'); should.not.exist(res.headers['x-cache-invalidate']); return models.Post.findOne({ diff --git a/core/test/acceptance/old/admin/utils.js b/core/test/acceptance/old/admin/utils.js index c26435b335..66df2ea630 100644 --- a/core/test/acceptance/old/admin/utils.js +++ b/core/test/acceptance/old/admin/utils.js @@ -33,6 +33,8 @@ const expectedProperties = { .without('author_id') // always returns computed properties .concat('url', 'primary_tag', 'primary_author', 'excerpt') + // returned by default + .concat('tags', 'authors') , page: _(schema.posts) @@ -46,6 +48,8 @@ const expectedProperties = { .without('author_id') // always returns computed properties .concat('url', 'primary_tag', 'primary_author', 'excerpt') + // returned by default + .concat('tags', 'authors') , user: _(schema.users) diff --git a/core/test/regression/api/v2/admin/utils.js b/core/test/regression/api/v2/admin/utils.js index 302a4ed1ec..364c1cd5fc 100644 --- a/core/test/regression/api/v2/admin/utils.js +++ b/core/test/regression/api/v2/admin/utils.js @@ -25,8 +25,10 @@ const expectedProperties = { .without('visibility') .without('locale') .without('page') - // always returns computed properties: url, comment_id, primary_tag, primary_author - .without('author_id').concat('url', 'primary_tag', 'primary_author') + .without('author_id') + // always returns computed properties + .concat('url', 'primary_tag', 'primary_author', 'excerpt') + .concat('authors', 'tags') , user: _(schema.users) .keys()