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): handle non-string values in Postman collection import (#6137)

okxint174c87d5d34c
diff --git a/packages/hoppscotch-common/src/helpers/__tests__/postman-import.spec.ts b/packages/hoppscotch-common/src/helpers/__tests__/postman-import.spec.ts
new file mode 100644
index 000000000..c1eb3c9ef
--- /dev/null
+++ b/packages/hoppscotch-common/src/helpers/__tests__/postman-import.spec.ts
@@ -0,0 +1,60 @@
+import * as E from "fp-ts/Either"
+import { describe, expect, it } from "vitest"
+
+import { hoppPostmanImporter } from "../import-export/import/postman"
+
+const postmanCollectionWithArrayHeader = JSON.stringify({
+ info: {
+ name: "Array Header Import",
+ schema:
+ "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
+ },
+ item: [
+ {
+ name: "Import Array Header",
+ request: {
+ method: "GET",
+ header: [
+ {
+ key: "Authorization",
+ value: ["Basic xxxxx", "Basic xxxxxxxxxxxx="],
+ },
+ {
+ key: "Content-Type",
+ value: "application/x-www-form-urlencoded",
+ },
+ ],
+ url: "https://echo.hoppscotch.io/get",
+ },
+ },
+ ],
+})
+
+describe("Postman importer", () => {
+ it("coerces array header values instead of crashing during import", async () => {
+ const result = await hoppPostmanImporter([
+ postmanCollectionWithArrayHeader,
+ ])()
+
+ expect(E.isRight(result)).toBe(true)
+
+ if (E.isLeft(result)) {
+ throw new Error("Expected Postman import to succeed")
+ }
+
+ expect(result.right[0].requests[0].headers).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ key: "Authorization",
+ value: "Basic xxxxx,Basic xxxxxxxxxxxx=",
+ active: true,
+ }),
+ expect.objectContaining({
+ key: "Content-Type",
+ value: "application/x-www-form-urlencoded",
+ active: true,
+ }),
+ ])
+ )
+ })
+})
diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/postman.ts b/packages/hoppscotch-common/src/helpers/import-export/import/postman.ts
index d07436332..e7cbc8ba9 100644
--- a/packages/hoppscotch-common/src/helpers/import-export/import/postman.ts
+++ b/packages/hoppscotch-common/src/helpers/import-export/import/postman.ts
@@ -63,10 +63,10 @@ const getCollectionSchema = (jsonStr: string): string | null => {
}
}
-export const replacePMVarTemplating = flow(
- S.replace(/{{\s*/g, "<<"),
- S.replace(/\s*}}/g, ">>")
-)
+export const replacePMVarTemplating = (value: unknown): string => {
+ const str = typeof value === "string" ? value : String(value ?? "")
+ return pipe(str, S.replace(/{{\s*/g, "<<"), S.replace(/\s*}}/g, ">>"))
+}
const PMRawLanguageOptionsToContentTypeMap: Record<
PMRawLanguage,