Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(template): do not partially resolve function arg objects with special keys #5670

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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