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

Imported functions invoked in .bicepparam files can refer to variables from their source template #16322

Merged
merged 1 commit into from
Feb 4, 2025
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
Imported functions invoked in .bicepparam files can refer to variable…
…s from their source template
  • Loading branch information
jeskew committed Feb 3, 2025
commit 3a4f502b8f1060063e511a7cad76b97e89436dc9
56 changes: 56 additions & 0 deletions src/Bicep.Core.IntegrationTests/CompileTimeImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2516,4 +2516,60 @@ public void Imports_that_enclose_type_fragments_are_supported()
}
"""));
}

[TestMethod]
public void Imported_functions_invoked_in_bicepparam_files_can_refer_to_variables_in_source_template()
{
var result = CompilationHelper.CompileParams(
("parameters.bicepparam", """
using 'main.bicep'

import { diagnosticSettings } from './common.bicep'

var location = 'uksouth'
var locationSlug = substring(location, 0, 3)

var env = readEnvironmentVariable('ENVIRONMENT', 'dev')
var diagSettings = diagnosticSettings(env)

param keyVaults = {
'key-vault': {
resourceGroupName: 'resource-group'
diagnosticSettings: diagSettings
}
}
"""),
("main.bicep", "param keyVaults object"),
("common.bicep", """
@export()
func diagnosticSettings(env string) object => {
eventHub: {
namespace: {
subscriptionId: subscriptions.default[env]
resourceGroupName: 'diag-rg'
name: 'diag-ehn'
}
name: 'metrics'
}
metrics: [
{
category: 'AllMetrics'
}
]
}

@export()
var subscriptions = {
default: {
'dev': 'xxx'
'uat': 'yyy'
'prd': 'zzz'
}
}
"""));

result.ExcludingLinterDiagnostics("no-unused-vars").Should().NotHaveAnyDiagnostics();
result.Parameters.Should().NotBeNull();
result.Parameters.Should().HaveValueAtPath("$.parameters.keyVaults.value['key-vault'].diagnosticSettings.eventHub.namespace.subscriptionId", "xxx");
}
}
2 changes: 2 additions & 0 deletions src/Bicep.Core/Emit/ParameterAssignmentEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private JToken EvaluateUserDefinedFunction(FunctionExpression expression, Functi
JToken evaluateFunction(Template template, string originalFunctionName)
{
var functionsLookup = template.GetFunctionDefinitions().ToOrdinalInsensitiveDictionary(x => x.Key, x => x.Function);
TemplateVariablesEvaluator variablesEvaluator = new(template);

var rewrittenExpression = new FunctionExpression(
$"{EmitConstants.UserDefinedFunctionsNamespace}.{originalFunctionName}",
Expand All @@ -81,6 +82,7 @@ JToken evaluateFunction(Template template, string originalFunctionName)
var helper = new TemplateExpressionEvaluationHelper
{
OnGetFunction = (name, _) => functionsLookup[name],
OnGetVariable = (name, _) => variablesEvaluator.GetEvaluatedVariableValue(name),
ValidationContext = SchemaValidationContext.ForTemplate(template),
};

Expand Down
Loading