Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix(common): OpenAPI response example status code bug (#4966)

Nivedin3cf286a443e5
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 1d4516ed2..2bdffdcfb 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 000000000..e2100ce88
--- /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
+ )
+}