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 David Marby
parent 2b802ceb0d
commit 6c7628d15c
2 changed files with 19 additions and 4 deletions

View File

@ -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');

View File

@ -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}`;
}