From 086736588aafae8cdd70401b91de37eca91fb1d2 Mon Sep 17 00:00:00 2001 From: Dimitri Mitropoulos Date: Wed, 9 Mar 2022 05:41:25 -0500 Subject: [PATCH] hotfix: invalid OpenAPI spec versions causing the app to crash (#4559) --- .../ui/components/__tests__/workspace-card.test.ts | 10 ++++++++-- .../app/ui/components/workspace-card.tsx | 13 +++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/insomnia-app/app/ui/components/__tests__/workspace-card.test.ts b/packages/insomnia-app/app/ui/components/__tests__/workspace-card.test.ts index cc21f896a..b0ac1fb7a 100644 --- a/packages/insomnia-app/app/ui/components/__tests__/workspace-card.test.ts +++ b/packages/insomnia-app/app/ui/components/__tests__/workspace-card.test.ts @@ -1,12 +1,18 @@ import { getVersionDisplayment } from '../workspace-card'; describe('getVersionDisplayment', () => { - it('returns falsey values as-is', () => { - expect(getVersionDisplayment(undefined)).toEqual(undefined); + it('returns null, undefined, and empty string as-is', () => { expect(getVersionDisplayment(null)).toEqual(null); + expect(getVersionDisplayment(undefined)).toEqual(undefined); expect(getVersionDisplayment('')).toEqual(''); }); + it('returns numbers as strings', () => { + expect(getVersionDisplayment(0)).toEqual('v0'); // important to make sure we handle, since `0` is falsey + expect(getVersionDisplayment(1)).toEqual('v1'); + expect(getVersionDisplayment(1.1)).toEqual('v1.1'); + }); + it('does not add a `v` if the string starts with one', () => { expect(getVersionDisplayment('v1')).toEqual('v1'); expect(getVersionDisplayment('victor')).toEqual('victor'); diff --git a/packages/insomnia-app/app/ui/components/workspace-card.tsx b/packages/insomnia-app/app/ui/components/workspace-card.tsx index b32a14b89..891201d66 100644 --- a/packages/insomnia-app/app/ui/components/workspace-card.tsx +++ b/packages/insomnia-app/app/ui/components/workspace-card.tsx @@ -33,11 +33,20 @@ export interface WorkspaceCardProps { onSelect: (workspaceId: string, activity: GlobalActivity) => void; } -export const getVersionDisplayment = (version?: string | null) => { - if (!version) { +/** note: numbers are not technically valid (and, indeed, we throw a lint error), but we need to handle this case otherwise a user will not be able to import a spec with a malformed version and even _see_ that it's got the error. */ +export const getVersionDisplayment = (version?: string | number | null) => { + if (version === null || version === undefined || version === '') { return version; } + if (typeof version === 'number') { + console.warn(`OpenAPI documents must not use number data types for $.info.version, found ${version}`); + version = String(version); + } else if (typeof version !== 'string') { + console.error('unable to parse spec version'); + return ''; + } + if (!version.startsWith('v')) { return `v${version}`; }