From 3cf286a443e5af1b8ff1237f947ac145c2399ac3 Mon Sep 17 00:00:00 2001 From: Nivedin <53208152+nivedin@users.noreply.github.com> Date: Tue, 8 Apr 2025 14:46:29 +0530 Subject: [PATCH] fix(common): OpenAPI response example status code bug (#4966) Co-authored-by: jamesgeorge007 <25279263+jamesgeorge007@users.noreply.github.com> --- .../src/helpers/import-export/import/openapi.ts | 7 ++++--- .../hoppscotch-common/src/helpers/utils/number.ts | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 packages/hoppscotch-common/src/helpers/utils/number.ts diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts b/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts index 1d4516ed..2bdffdcf 100644 --- a/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts +++ b/packages/hoppscotch-common/src/helpers/import-export/import/openapi.ts @@ -29,8 +29,9 @@ import * as TE from "fp-ts/TaskEither" import * as RA from "fp-ts/ReadonlyArray" import * as E from "fp-ts/Either" import { IMPORTER_INVALID_FILE_FORMAT } from "." -import { cloneDeep, isNumber } from "lodash-es" +import { cloneDeep } from "lodash-es" import { getStatusCodeReasonPhrase } from "~/helpers/utils/statusCodes" +import { isNumeric } from "~/helpers/utils/number" export const OPENAPI_DEREF_ERROR = "openapi/deref_error" as const @@ -172,7 +173,7 @@ const parseOpenAPIV3Responses = ( const name = response.description ?? key - const code = isNumber(key) ? Number(key) : 200 + const code = isNumeric(key) ? Number(key) : 200 const status = getStatusCodeReasonPhrase(code) @@ -230,7 +231,7 @@ const parseOpenAPIV2Responses = ( const name = response.description ?? key - const code = isNumber(Number(key)) ? Number(key) : 200 + const code = isNumeric(Number(key)) ? Number(key) : 200 const status = getStatusCodeReasonPhrase(code) const headers: HoppRESTHeader[] = [ diff --git a/packages/hoppscotch-common/src/helpers/utils/number.ts b/packages/hoppscotch-common/src/helpers/utils/number.ts new file mode 100644 index 00000000..e2100ce8 --- /dev/null +++ b/packages/hoppscotch-common/src/helpers/utils/number.ts @@ -0,0 +1,14 @@ +/** + * Check if a value is numeric — either a valid number or a numeric string + * + * @param value - The value to check + * @returns True if the value is numeric, false otherwise + */ +export const isNumeric = (value: unknown): boolean => { + if (typeof value === "number") return !Number.isNaN(value) // handle actual numbers + if (typeof value !== "string") return false // only process strings or numbers! + return ( + value.trim() !== "" && // ensure strings of whitespace fail + !isNaN(Number(value)) // use type coercion to parse the entirety of the string + ) +}