hotfix: invalid OpenAPI spec versions causing the app to crash (#4559)

This commit is contained in:
Dimitri Mitropoulos 2022-03-09 05:41:25 -05:00 committed by GitHub
parent 4c16305257
commit 086736588a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View File

@ -1,12 +1,18 @@
import { getVersionDisplayment } from '../workspace-card'; import { getVersionDisplayment } from '../workspace-card';
describe('getVersionDisplayment', () => { describe('getVersionDisplayment', () => {
it('returns falsey values as-is', () => { it('returns null, undefined, and empty string as-is', () => {
expect(getVersionDisplayment(undefined)).toEqual(undefined);
expect(getVersionDisplayment(null)).toEqual(null); expect(getVersionDisplayment(null)).toEqual(null);
expect(getVersionDisplayment(undefined)).toEqual(undefined);
expect(getVersionDisplayment('')).toEqual(''); 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', () => { it('does not add a `v` if the string starts with one', () => {
expect(getVersionDisplayment('v1')).toEqual('v1'); expect(getVersionDisplayment('v1')).toEqual('v1');
expect(getVersionDisplayment('victor')).toEqual('victor'); expect(getVersionDisplayment('victor')).toEqual('victor');

View File

@ -33,11 +33,20 @@ export interface WorkspaceCardProps {
onSelect: (workspaceId: string, activity: GlobalActivity) => void; onSelect: (workspaceId: string, activity: GlobalActivity) => void;
} }
export const getVersionDisplayment = (version?: string | null) => { /** 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. */
if (!version) { export const getVersionDisplayment = (version?: string | number | null) => {
if (version === null || version === undefined || version === '') {
return 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')) { if (!version.startsWith('v')) {
return `v${version}`; return `v${version}`;
} }