0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

🐛 Fixed field filtering for /tags/:id endpoints

refs #10512

- Fixed ability to fetch specific fields when fetching tag resource by id
- Also only returning `url` field when specified in `fields` parameter
This commit is contained in:
Nazar Gargol 2019-03-05 13:17:38 +08:00
parent 97cf337907
commit 62c4ae119d
5 changed files with 47 additions and 6 deletions

View file

@ -18,7 +18,7 @@ const mapUser = (model, frame) => {
const mapTag = (model, frame) => {
const jsonModel = model.toJSON ? model.toJSON(frame.options) : model;
url.forTag(model.id, jsonModel);
url.forTag(model.id, jsonModel, frame.options);
clean.tag(jsonModel, frame);
return jsonModel;

View file

@ -54,8 +54,10 @@ const forUser = (id, attrs) => {
return attrs;
};
const forTag = (id, attrs) => {
attrs.url = urlService.getUrlByResourceId(id, {absolute: true});
const forTag = (id, attrs, options) => {
if (!options.columns || (options.columns && options.columns.includes('url'))) {
attrs.url = urlService.getUrlByResourceId(id, {absolute: true});
}
if (attrs.feature_image) {
attrs.feature_image = urlService.utils.urlFor('image', {image: attrs.feature_image}, true);

View file

@ -81,7 +81,7 @@ Tag = ghostBookshelf.Model.extend({
// these are the only options that can be passed to Bookshelf / Knex.
validOptions = {
findAll: ['columns'],
findOne: ['visibility'],
findOne: ['columns', 'visibility'],
destroy: ['destroyAll']
};

View file

@ -0,0 +1,39 @@
const should = require('should');
const supertest = require('supertest');
const localUtils = require('./utils');
const testUtils = require('../../../../utils');
const configUtils = require('../../../../utils/configUtils');
const config = require('../../../../../server/config');
const ghost = testUtils.startGhost;
let request;
describe('Tags', function () {
before(function () {
return ghost()
.then(function () {
request = supertest.agent(config.get('url'));
})
.then(function () {
return testUtils.initFixtures('users:no-owner', 'user:inactive', 'posts', 'tags:extra', 'api_keys');
});
});
afterEach(function () {
configUtils.restore();
});
const validKey = localUtils.getValidKey();
it('can read tags with fields', function () {
return request
.get(localUtils.API.getApiQuery(`tags/${testUtils.DataGenerator.Content.tags[0].id}/?key=${validKey}&fields=name,slug`))
.set('Origin', testUtils.API.getURL())
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200)
.then((res)=> {
localUtils.API.checkResponse(res.body.tags[0], 'tag', null, null, ['id', 'name', 'slug']);
});
});
});

View file

@ -74,7 +74,7 @@ describe('Unit: v2/utils/serializers/output/utils/mapper', () => {
urlUtil.forTag.callCount.should.equal(1);
urlUtil.forUser.callCount.should.equal(1);
urlUtil.forTag.getCall(0).args.should.eql(['id3', {id: 'id3', feature_image: 'value'}]);
urlUtil.forTag.getCall(0).args.should.eql(['id3', {id: 'id3', feature_image: 'value'}, frame.options]);
urlUtil.forUser.getCall(0).args.should.eql(['id4', {name: 'Ghosty', id: 'id4'}]);
});
});
@ -131,7 +131,7 @@ describe('Unit: v2/utils/serializers/output/utils/mapper', () => {
mapper.mapTag(tag, frame);
urlUtil.forTag.callCount.should.equal(1);
urlUtil.forTag.getCall(0).args.should.eql(['id3', tag]);
urlUtil.forTag.getCall(0).args.should.eql(['id3', tag, frame.options]);
});
});