Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
fix(importer): coerce non-string values in Insomnia import templating (#6608)
Ayam2b8748b535c6
diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/__tests__/insomnia.spec.ts b/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/__tests__/insomnia.spec.tsnew file mode 100644index 000000000..c88fa53da--- /dev/null+++ b/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/__tests__/insomnia.spec.ts@@ -0,0 +1,99 @@+import { describe, expect, it } from "vitest"+import * as E from "fp-ts/Either"++import { insomniaEnvImporter, replaceInsomniaTemplating } from "../insomniaEnv"+import { hoppInsomniaImporter, replacePathVarTemplating } from "../insomniaColl"++describe("insomnia import templating", () => {+ it("coerces non-string templating inputs instead of crashing (#6606)", () => {+ expect(replaceInsomniaTemplating("{{ _.host }}:3000")).toBe("<<host>>:3000")+ expect(replaceInsomniaTemplating(123)).toBe("123")+ expect(replaceInsomniaTemplating(true)).toBe("true")+ expect(replaceInsomniaTemplating(undefined)).toBe("")+ })++ it("templates path-segment variables but leaves port numbers alone", () => {+ expect(replacePathVarTemplating("/api/v1/:id")).toBe("/api/v1/<<id>>")+ expect(replacePathVarTemplating("http://localhost:3000/api")).toBe(+ "http://localhost:3000/api"+ )+ expect(+ replacePathVarTemplating("https://api.example.com:8443/v1/:userId")+ ).toBe("https://api.example.com:8443/v1/<<userId>>")+ })++ it('preserves JSON null env values as the literal string "null"', async () => {+ const contents = [+ JSON.stringify({+ resources: [+ {+ _type: "environment",+ name: "test-env",+ data: { a: null, b: 123, c: true, d: "plain" },+ },+ ],+ }),+ ]++ const result = await insomniaEnvImporter(contents)()++ if (!E.isRight(result)) {+ throw new Error("expected the import to succeed")+ }++ const variables = result.right[0].variables+ const initialValues = Object.fromEntries(+ variables.map((v) => [v.key, v.initialValue])+ )++ expect(initialValues).toEqual({+ a: "null",+ b: "123",+ c: "true",+ d: "plain",+ })+ })++ it('preserves null v5 collection and folder env values as the literal string "null"', async () => {+ const contents = [+ JSON.stringify({+ _type: "export",+ __export_format: 5,+ type: "collection.insomnia.rest/5.0.1",+ name: "test-collection",+ meta: { id: "w_1", created: 1, modified: 1, description: null },+ environments: { data: { a: null, b: 123, c: true, d: "plain" } },+ collection: [+ {+ name: "folder",+ meta: { id: "f_1", created: 1, modified: 1, description: null },+ children: [],+ environment: { f: null, g: false },+ },+ ],+ }),+ ]++ const result = await hoppInsomniaImporter(contents)()++ if (!E.isRight(result)) {+ throw new Error("expected the import to succeed")+ }++ const [collection] = result.right+ const collectionValues = Object.fromEntries(+ collection.variables.map((v) => [v.key, v.initialValue])+ )+ expect(collectionValues).toEqual({+ a: "null",+ b: "123",+ c: "true",+ d: "plain",+ })++ const folderValues = Object.fromEntries(+ collection.folders[0].variables.map((v) => [v.key, v.initialValue])+ )+ expect(folderValues).toEqual({ f: "null", g: "false" })+ })+})diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/insomniaColl.ts b/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/insomniaColl.tsindex c2b84b16b..383493fa6 100644--- a/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/insomniaColl.ts+++ b/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/insomniaColl.ts@@ -43,10 +43,16 @@ const isV5InsomniaDoc = (data: InsomniaDoc) =>typeof data.type === "string" &&(data.type as string).startsWith("collection.insomnia.rest/5")-const replacePathVarTemplating = (expression: string) =>- expression.replaceAll(/:([^/]+)/g, "<<$1>>")+export const replacePathVarTemplating = (expression: unknown) => {+ // Same coercion as replaceInsomniaTemplating: v5 export values are typed as+ // strings but arrive as numbers/booleans/null in real files (#6606).+ const safe = String(expression ?? "")+ // The colon must start a path segment: a bare ":([^/]+)" pattern also+ // swallows port numbers in absolute URLs (http://localhost:3000 -> <<3000>>).+ return safe.replaceAll(/(?<=\/):([^/]+)/g, "<<$1>>")+}-const replaceVarTemplating = (expression: string, pathVar = false) => {+const replaceVarTemplating = (expression: unknown, pathVar = false) => {return pipe(expression,pathVar ? replacePathVarTemplating : (x) => x,@@ -80,7 +86,7 @@ const getRequestsIn = ()const getCollectionVariables = (- environment: Record<string, string> | undefined,+ environment: Record<string, unknown> | undefined,folderRes?: InsomniaFolderResource): HoppCollectionVariable[] => {const env =@@ -91,7 +97,10 @@ const getCollectionVariables = (return Object.entries(env).map(([key, value]) => ({key: replaceVarTemplating(key),currentValue: "", // set it as empty value since it is handled by currentValue service and we don't want it to sync with BE- initialValue: replaceVarTemplating(value),+ // Values in real v5 exports are not guaranteed to be strings (numbers,+ // booleans, null all show up); coerce so the import never crashes. Plain+ // String() keeps JSON null round-trippable as "null", like the env importer.+ initialValue: replaceVarTemplating(String(value)),secret: false,}))}diff --git a/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/insomniaEnv.ts b/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/insomniaEnv.tsindex d7ec3a044..65079b130 100644--- a/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/insomniaEnv.ts+++ b/packages/hoppscotch-common/src/helpers/import-export/import/insomnia/insomniaEnv.ts@@ -27,9 +27,14 @@ const insomniaEnvSchema = z.object({data: z.record(z.string()),})-export const replaceInsomniaTemplating = (expression: string) => {+export const replaceInsomniaTemplating = (expression: unknown) => {+ // Real-world Insomnia v5 exports carry non-string values in environment and+ // folder-environment maps (numbers, booleans, null). Coerce before+ // replaceAll so the importer degrades gracefully instead of crashing with+ // "replaceAll is not a function" mid-import (#6606).+ const safe = String(expression ?? "")const regex = /\{\{ _\.([^}]+) \}\}/g- return expression.replaceAll(regex, "<<$1>>")+ return safe.replaceAll(regex, "<<$1>>")}export const insomniaEnvImporter = (contents: string[]) => {