From c7385f00be0f638bc793369d4b988f9b5099bbdc Mon Sep 17 00:00:00 2001 From: Jack Kavanagh Date: Fri, 21 Jun 2024 12:36:31 +0200 Subject: [PATCH] normalise json path (#7575) * normalise json path * fix tests --- .../importers/__snapshots__/index.test.ts.snap | 6 +++--- .../utils/importers/importers/postman-env.ts | 4 +--- .../utils/importers/importers/postman.test.ts | 2 +- .../src/utils/importers/importers/postman.ts | 18 ++++++++++++------ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/insomnia/src/utils/importers/importers/__snapshots__/index.test.ts.snap b/packages/insomnia/src/utils/importers/importers/__snapshots__/index.test.ts.snap index 9373eced3..557fc78cd 100644 --- a/packages/insomnia/src/utils/importers/importers/__snapshots__/index.test.ts.snap +++ b/packages/insomnia/src/utils/importers/importers/__snapshots__/index.test.ts.snap @@ -5845,7 +5845,7 @@ exports[`Fixtures Import postman scripts-import-v2_1-input.json 1`] = ` "description": "", "environment": { "bob": "something", - "dssx": "{{env_var_in_global}}", + "dssx": "{{_['env-var-in-global']}}", }, "metaSortKey": -1622117984000, "name": "New Collection", @@ -5853,7 +5853,7 @@ exports[`Fixtures Import postman scripts-import-v2_1-input.json 1`] = ` "preRequestScript": "console.log('pre')", "variable": { "bob": "something", - "dssx": "{{env_var_in_global}}", + "dssx": "{{_['env-var-in-global']}}", }, }, { @@ -5907,7 +5907,7 @@ exports[`Fixtures Import postman-env no-name-input.json 1`] = ` "_id": "__ENV_1__", "_type": "environment", "data": { - "foo_and_bar": "production-env", + "foo-and-bar": "production-env", }, "name": "Postman Environment", "parentId": "__BASE_ENVIRONMENT_ID__", diff --git a/packages/insomnia/src/utils/importers/importers/postman-env.ts b/packages/insomnia/src/utils/importers/importers/postman-env.ts index 208026fa0..5bcfab417 100644 --- a/packages/insomnia/src/utils/importers/importers/postman-env.ts +++ b/packages/insomnia/src/utils/importers/importers/postman-env.ts @@ -39,11 +39,9 @@ export const convert: Converter = rawData => { if (!enabled) { return accumulator; } - // hyphenated keys are not allowed in nunjucks eg. {{ foo-bar }} -> {{ foo_bar }} - const transformedString = key.replace(/-/g, '_'); return { ...accumulator, - [transformedString]: value, + [key]: value, }; }, {}), }, diff --git a/packages/insomnia/src/utils/importers/importers/postman.test.ts b/packages/insomnia/src/utils/importers/importers/postman.test.ts index 7413117dd..39aaf4a70 100644 --- a/packages/insomnia/src/utils/importers/importers/postman.test.ts +++ b/packages/insomnia/src/utils/importers/importers/postman.test.ts @@ -41,7 +41,7 @@ describe('postman', () => { }); it('should transform hyphens to underscores', () => { const input = 'abc{{my-env-var}}def{{here-and-here}}ghi'; - const output = 'abc{{my_env_var}}def{{here_and_here}}ghi'; + const output = "abc{{_['my-env-var']}}def{{_['here-and-here']}}ghi"; expect(transformPostmanToNunjucksString(input)).toEqual(output); expect(transformPostmanToNunjucksString()).toEqual(''); }); diff --git a/packages/insomnia/src/utils/importers/importers/postman.ts b/packages/insomnia/src/utils/importers/importers/postman.ts index ab94a45a8..a33b79cbf 100644 --- a/packages/insomnia/src/utils/importers/importers/postman.ts +++ b/packages/insomnia/src/utils/importers/importers/postman.ts @@ -1,4 +1,5 @@ import { AuthTypeOAuth2 } from '../../../models/request'; +import { forceBracketNotation } from '../../../templating/utils'; import { fakerFunctions } from '../../../ui/components/templating/faker-functions'; import { Converter, ImportRequest, Parameter } from '../entities'; import { @@ -64,20 +65,25 @@ export const transformPostmanToNunjucksString = (inputString?: string | null) => if (typeof inputString !== 'string') { return inputString; } - const sanitizedString = replaceHyphens(inputString); - return postmanTagRegexs.reduce((transformedString, { tag, regex }) => { + const replaceFaker = postmanTagRegexs.reduce((transformedString, { tag, regex }) => { return transformedString.replace(regex, postmanToNunjucksLookup[tag]); - }, sanitizedString); + }, inputString); + return normaliseJsonPath(replaceFaker); }; -export const replaceHyphens = (input?: string) => { +// old: {{ arr-name-with-dash }} +// new: {{ _['arr-name-with-dash'] }} +export const normaliseJsonPath = (input?: string) => { if (!input) { return ''; } + if (!input.includes('-')) { + return input; + } // Use a regular expression to find and replace the pattern - return input.replace(/\{\{([^\}]+)\}\}/g, (_, match) => { + return input.replace(/{{\s*([^ }]+)\s*[^}]*\s*}}/g, (_, match) => { // Replace hyphens with underscores within the match - const replaced = match.replace(/-/g, '_'); + const replaced = forceBracketNotation('_', match); // Return the replaced pattern within the curly braces return `{{${replaced}}}`; });