diff --git a/plugins/importer-openapi/tests/index.test.ts b/plugins/importer-openapi/tests/index.test.ts index a409ae52..92a0e93f 100644 --- a/plugins/importer-openapi/tests/index.test.ts +++ b/plugins/importer-openapi/tests/index.test.ts @@ -7,6 +7,29 @@ describe('importer-openapi', () => { const p = path.join(__dirname, 'fixtures'); const fixtures = fs.readdirSync(p); + test('Maps operation description to request description', async () => { + const imported = await convertOpenApi( + JSON.stringify({ + openapi: '3.0.0', + info: { title: 'Description Test', version: '1.0.0' }, + paths: { + '/klanten': { + get: { + description: 'Lijst van klanten', + responses: { '200': { description: 'ok' } }, + }, + }, + }, + }), + ); + + expect(imported?.resources.httpRequests).toEqual([ + expect.objectContaining({ + description: 'Lijst van klanten', + }), + ]); + }); + test('Skips invalid file', async () => { const imported = await convertOpenApi('{}'); expect(imported).toBeUndefined(); diff --git a/plugins/importer-postman/src/index.ts b/plugins/importer-postman/src/index.ts index 7567815b..5aa957a6 100644 --- a/plugins/importer-postman/src/index.ts +++ b/plugins/importer-postman/src/index.ts @@ -55,19 +55,11 @@ export function convertPostman(contents: string): ImportPluginResponse | undefin folders: [], }; - const rawDescription = info.description; - const description = - typeof rawDescription === 'object' && rawDescription != null && 'content' in rawDescription - ? String(rawDescription.content) - : rawDescription == null - ? undefined - : String(rawDescription); - const workspace: ExportResources['workspaces'][0] = { model: 'workspace', id: generateId('workspace'), name: info.name ? String(info.name) : 'Postman Import', - description, + description: importDescription(info.description), ...globalAuth, }; exportResources.workspaces.push(workspace); @@ -139,7 +131,7 @@ export function convertPostman(contents: string): ImportPluginResponse | undefin workspaceId: workspace.id, folderId, name: v.name, - description: r.description ? String(r.description) : undefined, + description: importDescription(r.description), method: typeof r.method === 'string' ? r.method : 'GET', url, urlParameters, @@ -509,6 +501,26 @@ function toArray(value: unknown): T[] { return []; } +function importDescription(rawDescription: unknown): string | undefined { + if (rawDescription == null) { + return undefined; + } + + if (typeof rawDescription === 'string') { + return rawDescription; + } + + if (typeof rawDescription === 'object' && !Array.isArray(rawDescription)) { + const description = toRecord(rawDescription); + if ('content' in description && description.content != null) { + return String(description.content); + } + return undefined; + } + + return String(rawDescription); +} + /** Recursively render all nested object properties */ function convertTemplateSyntax(obj: T): T { if (typeof obj === 'string') { diff --git a/plugins/importer-postman/tests/index.test.ts b/plugins/importer-postman/tests/index.test.ts index f40cfdad..aecf8a4f 100644 --- a/plugins/importer-postman/tests/index.test.ts +++ b/plugins/importer-postman/tests/index.test.ts @@ -22,4 +22,39 @@ describe('importer-postman', () => { ); }); } + + test('Imports object descriptions without [object Object]', () => { + const result = convertPostman( + JSON.stringify({ + info: { + name: 'Description Test', + schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json', + }, + item: [ + { + name: 'Request 1', + request: { + method: 'GET', + description: { + content: 'Lijst van klanten', + type: 'text/plain', + }, + }, + }, + ], + }), + ); + + expect(result?.resources.workspaces).toEqual([ + expect.objectContaining({ + name: 'Description Test', + }), + ]); + expect(result?.resources.httpRequests).toEqual([ + expect.objectContaining({ + name: 'Request 1', + description: 'Lijst van klanten', + }), + ]); + }); });