fix: url interpolation in code gen (#5187)

This commit is contained in:
Pooja
2025-08-07 20:25:28 +05:30
committed by GitHub
parent 86901c1e89
commit d031687ee9
6 changed files with 14 additions and 60 deletions

View File

@@ -11,7 +11,7 @@ import {
import { interpolateUrl, interpolateUrlPathParams } from 'utils/url/index';
import { getLanguages } from 'utils/codegenerator/targets';
import { useSelector } from 'react-redux';
import { getGlobalEnvironmentVariables } from 'utils/collections/index';
import { getAllVariables, getGlobalEnvironmentVariables } from 'utils/collections/index';
import { resolveInheritedAuth } from './utils/auth-utils';
const GenerateCodeItem = ({ collectionUid, item, onClose }) => {
@@ -37,12 +37,11 @@ const GenerateCodeItem = ({ collectionUid, item, onClose }) => {
const requestUrl =
get(item, 'draft.request.url') !== undefined ? get(item, 'draft.request.url') : get(item, 'request.url');
const variables = getAllVariables(collection, item);
const interpolatedUrl = interpolateUrl({
url: requestUrl,
globalEnvironmentVariables,
envVars,
runtimeVariables: collection.runtimeVariables,
processEnvVars: collection.processEnvVariables
variables
});
// interpolate the path params

View File

@@ -69,24 +69,3 @@ export const interpolateBody = (body, variables = {}) => {
return interpolatedBody;
};
export const createVariablesObject = ({
globalEnvironmentVariables = {},
collectionVars = {},
allVariables = {},
collection = {},
runtimeVariables = {},
processEnvVars = {}
}) => {
return {
...globalEnvironmentVariables,
...allVariables,
...collectionVars,
...runtimeVariables,
process: {
env: {
...processEnvVars
}
}
};
};

View File

@@ -1,7 +1,7 @@
import { buildHarRequest } from 'utils/codegenerator/har';
import { getAuthHeaders } from 'utils/codegenerator/auth';
import { getAllVariables, getTreePathFromCollectionToItem } from 'utils/collections/index';
import { interpolateHeaders, interpolateBody, createVariablesObject } from './interpolation';
import { interpolateHeaders, interpolateBody } from './interpolation';
// Merge headers from collection, folders, and request
const mergeHeaders = (collection, request, requestTreePath) => {
@@ -46,17 +46,7 @@ const generateSnippet = ({ language, item, collection, shouldInterpolate = false
// Get HTTPSnippet dynamically so mocks can be applied in tests
const { HTTPSnippet } = require('httpsnippet');
const allVariables = getAllVariables(collection, item);
// Create variables object for interpolation
const variables = createVariablesObject({
globalEnvironmentVariables: collection.globalEnvironmentVariables || {},
collectionVars: collection.collectionVars || {},
allVariables,
collection,
runtimeVariables: collection.runtimeVariables || {},
processEnvVars: collection.processEnvVariables || {}
});
const variables = getAllVariables(collection, item);
const request = item.request;

View File

@@ -46,7 +46,10 @@ jest.mock('utils/codegenerator/auth', () => ({
}));
jest.mock('utils/collections/index', () => ({
getAllVariables: jest.fn(() => ({
getAllVariables: jest.fn((collection) => ({
...collection?.globalEnvironmentVariables,
...collection?.runtimeVariables,
...collection?.processEnvVariables,
baseUrl: 'https://api.example.com',
apiKey: 'secret-key-123',
userId: '12345'

View File

@@ -69,21 +69,12 @@ export const isValidUrl = (url) => {
}
};
export const interpolateUrl = ({ url, globalEnvironmentVariables = {}, envVars, runtimeVariables, processEnvVars }) => {
export const interpolateUrl = ({ url, variables }) => {
if (!url || !url.length || typeof url !== 'string') {
return;
}
return interpolate(url, {
...globalEnvironmentVariables,
...envVars,
...runtimeVariables,
process: {
env: {
...processEnvVars
}
}
});
return interpolate(url, variables);
};
export const interpolateUrlPathParams = (url, params) => {

View File

@@ -77,11 +77,7 @@ describe('Url Utils - interpolateUrl, interpolateUrlPathParams', () => {
const url = '{{host}}/api/:id/path?foo={{foo}}&bar={{bar}}&baz={{process.env.baz}}';
const expectedUrl = 'https://example.com/api/:id/path?foo=foo_value&bar=bar_value&baz=baz_value';
const envVars = { host: 'https://example.com', foo: 'foo_value' };
const runtimeVariables = { bar: 'bar_value' };
const processEnvVars = { baz: 'baz_value' };
const result = interpolateUrl({ url, envVars, runtimeVariables, processEnvVars });
const result = interpolateUrl({ url, variables: { host: 'https://example.com', foo: 'foo_value', bar: 'bar_value', 'process.env.baz': 'baz_value' } });
expect(result).toEqual(expectedUrl);
});
@@ -101,11 +97,7 @@ describe('Url Utils - interpolateUrl, interpolateUrlPathParams', () => {
const params = [{ name: 'id', type: 'path', enabled: true, value: '123' }];
const expectedUrl = 'https://example.com/api/123/path?foo=foo_value&bar=bar_value&baz=baz_value';
const envVars = { host: 'https://example.com', foo: 'foo_value' };
const runtimeVariables = { bar: 'bar_value' };
const processEnvVars = { baz: 'baz_value' };
const intermediateResult = interpolateUrl({ url, envVars, runtimeVariables, processEnvVars });
const intermediateResult = interpolateUrl({ url, variables: { host: 'https://example.com', foo: 'foo_value', bar: 'bar_value', 'process.env.baz': 'baz_value' } });
const result = interpolateUrlPathParams(intermediateResult, params);
expect(result).toEqual(expectedUrl);