Skip to content

Commit

Permalink
normalise json path (Kong#7575)
Browse files Browse the repository at this point in the history
* normalise json path

* fix tests
  • Loading branch information
jackkav authored and stefancruz committed Jun 30, 2024
1 parent 13b8532 commit 2de8ca7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5845,15 +5845,15 @@ 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",
"parentId": "__WORKSPACE_ID__",
"preRequestScript": "console.log('pre')",
"variable": {
"bob": "something",
"dssx": "{{env_var_in_global}}",
"dssx": "{{_['env-var-in-global']}}",
},
},
{
Expand Down Expand Up @@ -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__",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ export const convert: Converter<Data> = 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,
};
}, {}),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
});
Expand Down
18 changes: 12 additions & 6 deletions packages/insomnia/src/utils/importers/importers/postman.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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}}}`;
});
Expand Down

0 comments on commit 2de8ca7

Please sign in to comment.