mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 03:14:03 +03:00
Fixed fromDTO collection mapping
refs https://github.com/TryGhost/Team/issues/3260 - Whenever properties of the DTO are undefined they should be removed to avoid unintentional empty assignments to the stored collection
This commit is contained in:
parent
a0a7c3a61b
commit
253f144501
@ -74,13 +74,22 @@ export class CollectionsService {
|
||||
}
|
||||
|
||||
fromDTO(data: any): any {
|
||||
return {
|
||||
const mappedDTO: {[index: string]:any} = {
|
||||
title: data.title,
|
||||
slug: data.slug,
|
||||
description: data.description,
|
||||
featureImage: data.feature_image,
|
||||
filter: data.filter
|
||||
};
|
||||
|
||||
// delete out keys that contain undefined values
|
||||
for (const key of Object.keys(mappedDTO)) {
|
||||
if (mappedDTO[key] === undefined) {
|
||||
delete mappedDTO[key];
|
||||
}
|
||||
}
|
||||
|
||||
return mappedDTO;
|
||||
}
|
||||
|
||||
async createCollection(data: CollectionInputDTO): Promise<CollectionDTO> {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import assert from 'assert';
|
||||
import {CollectionsService, CollectionsRepositoryInMemory} from '../src/index';
|
||||
import {CollectionsService, CollectionsRepositoryInMemory, Collection} from '../src/index';
|
||||
import {posts} from './fixtures/posts';
|
||||
|
||||
describe('CollectionsService', function () {
|
||||
@ -40,6 +40,16 @@ describe('CollectionsService', function () {
|
||||
assert.equal(deletedCollection, null, 'Collection should be deleted');
|
||||
});
|
||||
|
||||
describe('toDTO', function () {
|
||||
it('Can map Collection entity to DTO object', async function () {
|
||||
const collection = await Collection.create({});
|
||||
const dto = collectionsService.toDTO(collection);
|
||||
|
||||
assert.equal(dto.id, collection.id, 'DTO should have the same id as the entity');
|
||||
assert.equal(dto.title, null, 'DTO should return null if nullable property of the entity is unassigned');
|
||||
});
|
||||
});
|
||||
|
||||
describe('addPostToCollection', function () {
|
||||
it('Can add a Post to a Collection', async function () {
|
||||
const collection = await collectionsService.createCollection({
|
||||
|
Loading…
Reference in New Issue
Block a user