normalise json path (#7575)

* normalise json path

* fix tests
This commit is contained in:
Jack Kavanagh 2024-06-21 12:36:31 +02:00 committed by GitHub
parent 3fec8698d1
commit c7385f00be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 13 deletions

View File

@ -5845,7 +5845,7 @@ exports[`Fixtures Import postman scripts-import-v2_1-input.json 1`] = `
"description": "", "description": "",
"environment": { "environment": {
"bob": "something", "bob": "something",
"dssx": "{{env_var_in_global}}", "dssx": "{{_['env-var-in-global']}}",
}, },
"metaSortKey": -1622117984000, "metaSortKey": -1622117984000,
"name": "New Collection", "name": "New Collection",
@ -5853,7 +5853,7 @@ exports[`Fixtures Import postman scripts-import-v2_1-input.json 1`] = `
"preRequestScript": "console.log('pre')", "preRequestScript": "console.log('pre')",
"variable": { "variable": {
"bob": "something", "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__", "_id": "__ENV_1__",
"_type": "environment", "_type": "environment",
"data": { "data": {
"foo_and_bar": "production-env", "foo-and-bar": "production-env",
}, },
"name": "Postman Environment", "name": "Postman Environment",
"parentId": "__BASE_ENVIRONMENT_ID__", "parentId": "__BASE_ENVIRONMENT_ID__",

View File

@ -39,11 +39,9 @@ export const convert: Converter<Data> = rawData => {
if (!enabled) { if (!enabled) {
return accumulator; return accumulator;
} }
// hyphenated keys are not allowed in nunjucks eg. {{ foo-bar }} -> {{ foo_bar }}
const transformedString = key.replace(/-/g, '_');
return { return {
...accumulator, ...accumulator,
[transformedString]: value, [key]: value,
}; };
}, {}), }, {}),
}, },

View File

@ -41,7 +41,7 @@ describe('postman', () => {
}); });
it('should transform hyphens to underscores', () => { it('should transform hyphens to underscores', () => {
const input = 'abc{{my-env-var}}def{{here-and-here}}ghi'; 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(input)).toEqual(output);
expect(transformPostmanToNunjucksString()).toEqual(''); expect(transformPostmanToNunjucksString()).toEqual('');
}); });

View File

@ -1,4 +1,5 @@
import { AuthTypeOAuth2 } from '../../../models/request'; import { AuthTypeOAuth2 } from '../../../models/request';
import { forceBracketNotation } from '../../../templating/utils';
import { fakerFunctions } from '../../../ui/components/templating/faker-functions'; import { fakerFunctions } from '../../../ui/components/templating/faker-functions';
import { Converter, ImportRequest, Parameter } from '../entities'; import { Converter, ImportRequest, Parameter } from '../entities';
import { import {
@ -64,20 +65,25 @@ export const transformPostmanToNunjucksString = (inputString?: string | null) =>
if (typeof inputString !== 'string') { if (typeof inputString !== 'string') {
return inputString; return inputString;
} }
const sanitizedString = replaceHyphens(inputString); const replaceFaker = postmanTagRegexs.reduce((transformedString, { tag, regex }) => {
return postmanTagRegexs.reduce((transformedString, { tag, regex }) => {
return transformedString.replace(regex, postmanToNunjucksLookup[tag]); 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) { if (!input) {
return ''; return '';
} }
if (!input.includes('-')) {
return input;
}
// Use a regular expression to find and replace the pattern // 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 // 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 the replaced pattern within the curly braces
return `{{${replaced}}}`; return `{{${replaced}}}`;
}); });