Skip to content

Commit 72cc7a2

Browse files
authored
Merge pull request #3265 from Dokploy/feat/templates-processor-allow-empty-variables-references
test(helpers): add tests for handling empty and undefined string vari…
2 parents 0b45b79 + d875e08 commit 72cc7a2

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

apps/dokploy/__test__/templates/helpers.template.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,50 @@ describe("helpers functions", () => {
161161
});
162162
});
163163

164+
describe("Empty string variables", () => {
165+
it("should replace variables with empty string values correctly", () => {
166+
const variables = {
167+
smtp_username: "",
168+
smtp_password: "",
169+
non_empty: "value",
170+
};
171+
172+
const result1 = processValue("${smtp_username}", variables, mockSchema);
173+
expect(result1).toBe("");
174+
175+
const result2 = processValue("${smtp_password}", variables, mockSchema);
176+
expect(result2).toBe("");
177+
178+
const result3 = processValue("${non_empty}", variables, mockSchema);
179+
expect(result3).toBe("value");
180+
});
181+
182+
it("should not replace undefined variables", () => {
183+
const variables = {
184+
defined_var: "",
185+
};
186+
187+
const result = processValue("${undefined_var}", variables, mockSchema);
188+
expect(result).toBe("${undefined_var}");
189+
});
190+
191+
it("should handle mixed empty and non-empty variables in template", () => {
192+
const variables = {
193+
smtp_address: "smtp.example.com",
194+
smtp_port: "2525",
195+
smtp_username: "",
196+
smtp_password: "",
197+
};
198+
199+
const template =
200+
"SMTP_ADDRESS=${smtp_address} SMTP_PORT=${smtp_port} SMTP_USERNAME=${smtp_username} SMTP_PASSWORD=${smtp_password}";
201+
const result = processValue(template, variables, mockSchema);
202+
expect(result).toBe(
203+
"SMTP_ADDRESS=smtp.example.com SMTP_PORT=2525 SMTP_USERNAME= SMTP_PASSWORD=",
204+
);
205+
});
206+
});
207+
164208
describe("${jwt}", () => {
165209
it("should generate a JWT string", () => {
166210
const jwt = processValue("${jwt}", {}, mockSchema);

packages/server/src/templates/processors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ export function processValue(
170170
}
171171

172172
// If not a utility function, try to get from variables
173-
return variables[varName] || match;
173+
return varName in variables ? (variables[varName] ?? match) : match;
174174
});
175175

176176
// Then replace any remaining ${var} with their values from variables
177177
processedValue = processedValue.replace(/\${([^}]+)}/g, (match, varName) => {
178-
return variables[varName] || match;
178+
return varName in variables ? (variables[varName] ?? match) : match;
179179
});
180180

181181
return processedValue;

0 commit comments

Comments
 (0)