diff --git a/server/src/services/tag.service.spec.ts b/server/src/services/tag.service.spec.ts index de270777b0..a479a09fbb 100644 --- a/server/src/services/tag.service.spec.ts +++ b/server/src/services/tag.service.spec.ts @@ -140,6 +140,23 @@ describe(TagService.name, () => { parent: expect.objectContaining({ id: 'tag-parent' }), }); }); + + it('should upsert a tag and ignore leading and trailing slashes', async () => { + tagMock.getByValue.mockResolvedValueOnce(null); + tagMock.upsertValue.mockResolvedValueOnce(tagStub.parent); + tagMock.upsertValue.mockResolvedValueOnce(tagStub.child); + await expect(sut.upsert(authStub.admin, { tags: ['/Parent/Child/'] })).resolves.toBeDefined(); + expect(tagMock.upsertValue).toHaveBeenNthCalledWith(1, { + value: 'Parent', + userId: 'admin_id', + parent: undefined, + }); + expect(tagMock.upsertValue).toHaveBeenNthCalledWith(2, { + value: 'Parent/Child', + userId: 'admin_id', + parent: expect.objectContaining({ id: 'tag-parent' }), + }); + }); }); describe('remove', () => { diff --git a/server/src/utils/tag.ts b/server/src/utils/tag.ts index 6d6c70f1d7..027afcf040 100644 --- a/server/src/utils/tag.ts +++ b/server/src/utils/tag.ts @@ -8,7 +8,7 @@ export const upsertTags = async (repository: ITagRepository, { userId, tags }: U const results: TagEntity[] = []; for (const tag of tags) { - const parts = tag.split('/'); + const parts = tag.split('/').filter(Boolean); let parent: TagEntity | undefined; for (const part of parts) {