Skip to content

fix and add test for invalid parameter type #17193

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
62 changes: 62 additions & 0 deletions src/Bicep.Cli.IntegrationTests/BuildParamsCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,5 +640,67 @@ param intParam int
File.Exists(Path.Combine(bicepOutputPath, outputFile)).Should().Be(true, f);
}
}

[TestMethod]
public async Task BuildParams_Extends_InvalidType_ThrowsError()
{
var outputPath = FileHelper.GetUniqueTestOutputPath(TestContext);
FileHelper.SaveResultFile(TestContext, "main.bicep", @"
param tag string
", outputPath);
FileHelper.SaveResultFile(TestContext, "base.bicepparam", @"
using none
param tag = 42
", outputPath);
var inputFile = FileHelper.SaveResultFile(TestContext, "main.bicepparam", @"
using 'main.bicep'
extends 'base.bicepparam'
", outputPath);

var expectedOutputFile = FileHelper.GetResultFilePath(TestContext, "main.json", outputPath);
File.Exists(expectedOutputFile).Should().BeFalse();

var (output, error, result) = await Bicep(["build-params", inputFile]);

File.Exists(expectedOutputFile).Should().BeFalse();

output.Should().BeEmpty();
error.Should().Contain("Error BCP033: Expected a value of type \"string\" but the provided value is of type \"42\".");
result.Should().Be(1);
}

[TestMethod]
public async Task BuildParams_Extends_Multiple_InvalidType_ThrowsMultipleErrors()
{
var outputPath = FileHelper.GetUniqueTestOutputPath(TestContext);
FileHelper.SaveResultFile(TestContext, "main.bicep", @"
param myString string
param myInt int
param myBool bool
", outputPath);
FileHelper.SaveResultFile(TestContext, "base.bicepparam", @"
using none
param myInt = '42'
param myString = {}
param myBool = []
", outputPath);
var inputFile = FileHelper.SaveResultFile(TestContext, "main.bicepparam", @"
using './main.bicep'
extends 'base.bicepparam'
", outputPath);

var expectedOutputFile = FileHelper.GetResultFilePath(TestContext, "main.json", outputPath);
File.Exists(expectedOutputFile).Should().BeFalse();

var (output, error, result) = await Bicep(["build-params", inputFile]);

File.Exists(expectedOutputFile).Should().BeFalse();

output.Should().BeEmpty();
error.Should().Contain("Error BCP033: Expected a value of type \"int\" but the provided value is of type \"'42'\".");
error.Should().Contain("Error BCP033: Expected a value of type \"string\" but the provided value is of type \"object\".");
error.Should().Contain("Error BCP033: Expected a value of type \"bool\" but the provided value is of type \"<empty array>\".");
result.Should().Be(1);
}
}
}
6 changes: 3 additions & 3 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7014,7 +7014,7 @@ public void DependsOn_order_is_deterministic_for_hierarchical_resource_symbolic_
}
tenantId: '00000000-0000-0000-0000-000000000000'
}

resource secret 'secrets' = {
name: 'secret'
properties: {}
Expand All @@ -7031,7 +7031,7 @@ public void DependsOn_order_is_deterministic_for_hierarchical_resource_symbolic_
}
tenantId: '00000000-0000-0000-0000-000000000000'
}

resource secret 'secrets' = {
name: 'secret'
properties: {}
Expand Down Expand Up @@ -7067,7 +7067,7 @@ public void DependsOn_on_existing_resource_triggers_languageVersion_2()
module empty 'empty.bicep' = {
name: 'foo'
}

resource sa 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
name: 'storage'
dependsOn: [
Expand Down
13 changes: 13 additions & 0 deletions src/Bicep.Core/Emit/EmitLimitationCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Bicep.Core.Navigation;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Metadata;
using Bicep.Core.SourceGraph;
using Bicep.Core.Syntax;
using Bicep.Core.Syntax.Visitors;
using Bicep.Core.Text;
Expand Down Expand Up @@ -666,6 +667,18 @@ private static ImmutableDictionary<ParameterAssignmentSymbol, ParameterAssignmen
}
}

// TypeValidator.NarrowTypeAndCollectDiagnostics(model.TypeManager, model.Binder, model.SourceFile.ParsingErrorLookup, diagnostics, symbol.DeclaringSyntax, paramDefinitionFromBicepFile.Value.TypeReference.Type);
var bicepFile = model.SourceFileGrouping.SourceFiles.FirstOrDefault(sourceFile => sourceFile is BicepFile);
if (bicepFile is not null)
{
var paramDefinitionFromBicepFile = model.ModelLookup.GetSemanticModel(bicepFile).Parameters.FirstOrDefault(e => e.Key == symbol.Name);

if (paramDefinitionFromBicepFile.Value is not null && !TypeValidator.AreTypesAssignable(paramDefinitionFromBicepFile.Value.TypeReference.Type, symbol.Type))
{
diagnostics.WriteMultiple(DiagnosticBuilder.ForPosition(symbol.NameSource).ExpectedValueTypeMismatch(false, paramDefinitionFromBicepFile.Value.TypeReference.Type, symbol.Type));
}
}

if (referencedValueHasError)
{
erroredSymbols.Add(symbol);
Expand Down
Loading