fix: handle falsy values in Postman environment and collection variables (#4924)

* fix: handle falsy values in Postman environment and collection variables

* Updated the `postman-env-to-bruno-env` and `postman-to-bruno` converters to handle cases where variable keys or values are falsy, ensuring they default to empty strings.
* Added unit tests to verify the correct handling of falsy values in both environment and collection variables.

* fix: filter out null/undefined keys and values in Postman variable imports

* Updated the `postman-env-to-bruno-env` and `postman-to-bruno` converters to filter out variables with null keys and values during import.
* Removed redundant test cases for empty variables in the corresponding unit tests.
This commit is contained in:
sanish chirayath
2025-06-24 15:58:29 +05:30
committed by GitHub
parent bbf4ad6b98
commit 2bbfb28090
4 changed files with 153 additions and 7 deletions

View File

@@ -6,14 +6,14 @@ const isSecret = (type) => {
return type === 'secret';
};
const importPostmanEnvironmentVariables = (brunoEnvironment, values) => {
const importPostmanEnvironmentVariables = (brunoEnvironment, values = []) => {
brunoEnvironment.variables = brunoEnvironment.variables || [];
each(values, (i) => {
each(values.filter(i => !(i.key == null && i.value == null)), (i) => {
const brunoEnvironmentVariable = {
uid: uuid(),
name: i.key.replace(invalidVariableCharacterRegex, '_'),
value: i.value,
name: (i.key ?? '').replace(invalidVariableCharacterRegex, '_'),
value: i.value ?? '',
enabled: i.enabled,
secret: isSecret(i.type)
};

View File

@@ -119,10 +119,10 @@ const importScriptsFromEvents = (events, requestObject) => {
};
const importCollectionLevelVariables = (variables, requestObject) => {
const vars = variables.map((v) => ({
const vars = variables.filter(v => !(v.key == null && v.value == null)).map((v) => ({
uid: uuid(),
name: v.key.replace(invalidVariableCharacterRegex, '_'),
value: v.value,
name: (v.key ?? '').replace(invalidVariableCharacterRegex, '_'),
value: v.value ?? '',
enabled: true
}));

View File

@@ -47,6 +47,66 @@ describe('postmanToBrunoEnvironment Function', () => {
expect(brunoEnvironment).toEqual(expectedEnvironment);
});
it('should handle falsy values in environment variables', async () => {
const postmanEnvironment = {
"id": "some-id",
"name": "My Environment",
"values": [
{
"enabled": true,
"type": "text"
},
{
"value": "",
"enabled": true,
"type": "text"
},
{
"key": "",
"enabled": true,
"type": "text"
},
{
"key": "",
"value": "",
"enabled": true,
"type": "text"
}
]
};
const brunoEnvironment = await postmanToBrunoEnvironment(postmanEnvironment);
const expectedEnvironment = {
name: 'My Environment',
variables: [
{
name: '',
value: '',
enabled: true,
secret: false,
uid: "mockeduuidvalue123456",
},
{
name: '',
value: '',
enabled: true,
secret: false,
uid: "mockeduuidvalue123456",
},
{
name: '',
value: '',
enabled: true,
secret: false,
uid: "mockeduuidvalue123456",
}
],
};
expect(brunoEnvironment).toEqual(expectedEnvironment);
});
it.skip('should throw Error when JSON parsing fails', async () => {
const invalidBrunoEnvironment = {
"id": "some-id",
@@ -66,4 +126,23 @@ describe('postmanToBrunoEnvironment Function', () => {
'Unable to parse the postman environment json file'
);
});
it("should handle empty variables", async () => {
const collectionWithEmptyVars = {
"name": "My Environment",
"values": []
};
const brunoCollection = await postmanToBrunoEnvironment(collectionWithEmptyVars);
expect(brunoCollection.variables).toEqual([]);
});
it("should handle undefined variables", async () => {
const collectionWithUndefinedVars = {
"name": "My Environment",
};
const brunoCollection = await postmanToBrunoEnvironment(collectionWithUndefinedVars);
expect(brunoCollection.variables).toEqual([]);
});
});

View File

@@ -6,6 +6,73 @@ describe('postman-collection', () => {
const brunoCollection = await postmanToBruno(postmanCollection);
expect(brunoCollection).toMatchObject(expectedOutput);
});
it('should handle falsy values in collection variables', async () => {
const collectionWithFalsyVars = {
"info": {
"_postman_id": "7f91bbd8-cb97-41ac-8d0b-e1fcd8bb4ce9",
"name": "collection with falsy vars",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"variable": [
{
"type": "string"
},
{
"key": "",
"type": "string"
},
{
"value": "",
"type": "string"
},
{
"key": "",
"value": "",
"type": "string"
}
],
"item": []
};
const brunoCollection = await postmanToBruno(collectionWithFalsyVars);
expect(brunoCollection.root.request.vars.req).toEqual([
{
uid: "mockeduuidvalue123456",
name: '',
value: '',
enabled: true
},
{
uid: "mockeduuidvalue123456",
name: '',
value: '',
enabled: true
},
{
uid: "mockeduuidvalue123456",
name: '',
value: '',
enabled: true
}
]);
});
it("should handle empty variables", async () => {
const collectionWithEmptyVars = {
"info": {
"_postman_id": "7f91bbd8-cb97-41ac-8d0b-e1fcd8bb4ce9",
"name": "collection with falsy vars",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"variable": [],
"item": []
};
const brunoCollection = await postmanToBruno(collectionWithEmptyVars);
expect(brunoCollection.root.request.vars.req).toEqual([]);
});
});
// Simple Collection (postman)