Skip to content

Commit

Permalink
fix(template): do not partially resolve function arg objects with spe…
Browse files Browse the repository at this point in the history
…cial keys (#5670)

This caused bad handling of e.g. $forEach objects in input variables for
templated actions.
  • Loading branch information
edvald authored Jan 26, 2024
1 parent 1979e0e commit 5b7aea4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions core/src/template-string/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { validateSchema } from "../config/validation.js"
import { load, loadAll } from "js-yaml"
import { safeDumpYaml } from "../util/serialization.js"
import indentString from "indent-string"
import { maybeTemplateString } from "./template-string.js"
import { mayContainTemplateString } from "./template-string.js"

interface ExampleArgument {
input: any[]
Expand Down Expand Up @@ -510,12 +510,12 @@ export function callHelperFunction({
})

// do not apply helper function for an unresolved template string
if (maybeTemplateString(value)) {
if (mayContainTemplateString(value)) {
if (allowPartial) {
return { resolved: "${" + text + "}" }
} else {
const _error = new TemplateStringError({
message: `Function '${functionName}' cannot be applied on unresolved string`,
message: `Function '${functionName}' cannot be applied on unresolved value`,
})
return { _error }
}
Expand Down
14 changes: 12 additions & 2 deletions core/test/unit/src/template-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,8 +1191,7 @@ describe("resolveTemplateString", () => {

it("throws if the function fails", () => {
void expectError(() => resolveTemplateString({ string: "${jsonDecode('{]}')}", context: new TestContext({}) }), {
contains:
"Invalid template string (${jsonDecode('{]}')}): Error from helper function jsonDecode: SyntaxError: Expected property name or '}' in JSON at position 1 (line 1 column 2)",
contains: "Invalid template string (${jsonDecode('{]}')}): Error from helper function jsonDecode: SyntaxError",
})
})

Expand All @@ -1207,6 +1206,17 @@ describe("resolveTemplateString", () => {
expect(res).to.equal("${base64Encode('${environment.namespace}')}")
})

it("does not apply helper function on unresolved template object and returns string as-is, when allowPartial=true", () => {
const res = resolveTemplateString({
string: "${base64Encode(var.foo)}",
context: new TestContext({ foo: { $forEach: ["a", "b"], $return: "${item.value}" } }),
contextOpts: {
allowPartial: true,
},
})
expect(res).to.equal("${base64Encode(var.foo)}")
})

context("concat", () => {
it("allows empty strings", () => {
const res = resolveTemplateString({ string: "${concat('', '')}", context: new TestContext({}) })
Expand Down

0 comments on commit 5b7aea4

Please sign in to comment.