Skip to content

Commit

Permalink
Fix bad string escaping in ARM template generation (#2006)
Browse files Browse the repository at this point in the history
* Add negative test for issue 1993

* ...and fix the test!

* Update test baselines

Co-authored-by: Bicep Automation <bicep@noreply.github.com>
  • Loading branch information
anthony-c-martin and Bicep Automation authored Mar 25, 2021
1 parent 46343e0 commit 8871afd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Deployments.Templates" Version="1.0.162" />
<PackageReference Include="Azure.Deployments.Templates" Version="1.0.168" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
Expand Down
89 changes: 89 additions & 0 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Bicep.Core.IntegrationTests
public class ScenarioTests
{
[TestMethod]
// https://github.com/azure/bicep/issues/746
public void Test_Issue746()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -32,6 +33,7 @@ param l
}

[TestMethod]
// https://github.com/azure/bicep/issues/801
public void Test_Issue801()
{
var result = CompilationHelper.Compile(
Expand Down Expand Up @@ -93,6 +95,7 @@ param name string
}

[TestMethod]
// https://github.com/azure/bicep/issues/982
public void Test_Issue982()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -117,6 +120,7 @@ param serverFarmId string
}

[TestMethod]
// https://github.com/azure/bicep/issues/1093
public void Test_Issue1093()
{
var result = CompilationHelper.Compile(
Expand Down Expand Up @@ -156,6 +160,7 @@ param rgName string
}

[TestMethod]
// https://github.com/azure/bicep/issues/1173
public void Test_Issue1173()
{
var result = CompilationHelper.Compile(
Expand Down Expand Up @@ -310,6 +315,7 @@ param rtName string
}

[TestMethod]
// https://github.com/azure/bicep/issues/1185
public void Test_Issue1185()
{
var result = CompilationHelper.Compile(
Expand Down Expand Up @@ -338,6 +344,7 @@ param mgName string
}

[TestMethod]
// https://github.com/azure/bicep/issues/1332
public void Test_Issue1332()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -354,6 +361,7 @@ public void Test_Issue1332()
}

[TestMethod]
// https://github.com/azure/bicep/issues/486
public void Test_Issue486()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -371,6 +379,7 @@ public void Test_Issue486()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1362
public void Test_Issue1362_1()
{
var result = CompilationHelper.Compile(
Expand All @@ -390,6 +399,7 @@ public void Test_Issue1362_1()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1362
public void Test_Issue1362_2()
{
var result = CompilationHelper.Compile(
Expand All @@ -409,6 +419,7 @@ public void Test_Issue1362_2()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1402
public void Test_Issue1402()
{
var result = CompilationHelper.Compile(
Expand All @@ -429,6 +440,7 @@ public void Test_Issue1402()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1391
public void Test_Issue1391()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -449,6 +461,7 @@ public void Test_Issue1391()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1454
public void Test_Issue1454()
{
var result = CompilationHelper.Compile(
Expand Down Expand Up @@ -513,6 +526,7 @@ param location string
}

[TestMethod]
// https://github.com/azure/bicep/issues/1465
public void Test_Issue1465()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -526,6 +540,7 @@ public void Test_Issue1465()
}

[TestMethod]
// https://github.com/azure/bicep/issues/822
public void Test_Issue822()
{
var result = CompilationHelper.Compile(
Expand Down Expand Up @@ -558,6 +573,7 @@ param name string
}

[TestMethod]
// https://github.com/azure/bicep/issues/822
public void Test_Issue822_scoped()
{
var result = CompilationHelper.Compile(
Expand Down Expand Up @@ -587,6 +603,7 @@ param name string
}

[TestMethod]
// https://github.com/azure/bicep/issues/1388
public void Test_Issue1388()
{
var result = CompilationHelper.Compile(@"
Expand Down Expand Up @@ -644,6 +661,7 @@ param groupReaderId string
}

[TestMethod]
// https://github.com/azure/bicep/issues/1364
public void Test_Issue1364()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -657,6 +675,7 @@ public void Test_Issue1364()
}

[TestMethod]
// https://github.com/azure/bicep/issues/569
public void Test_Issue569_success()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -673,6 +692,7 @@ param myparam string
}

[TestMethod]
// https://github.com/azure/bicep/issues/569
public void Test_Issue569_duplicates()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -688,6 +708,7 @@ public void Test_Issue569_duplicates()
}

[TestMethod]
// https://github.com/azure/bicep/issues/569
public void Test_Issue569_outputs_cannot_be_referenced()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -702,6 +723,7 @@ public void Test_Issue569_outputs_cannot_be_referenced()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1599
public void Test_Issue1599()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -716,6 +738,7 @@ public void Test_Issue1599()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1661
public void Test_Issue1661()
{
// Issue 1661 only repros if global-resources.bicep exists and kevault-secrets.bicep does not
Expand Down Expand Up @@ -804,6 +827,7 @@ param tags object
}

[TestMethod]
// https://github.com/azure/bicep/issues/1592
public void Test_Issue1592()
{
var result = CompilationHelper.Compile(
Expand All @@ -821,6 +845,7 @@ public void Test_Issue1592()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1592
public void Test_Issue1592_special_cases()
{
var result = CompilationHelper.Compile(
Expand All @@ -844,6 +869,7 @@ param someParam string
}

[TestMethod]
// https://github.com/azure/bicep/issues/1432
public void Test_Issue1432()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -861,6 +887,7 @@ public void Test_Issue1432()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1817
public void Test_Issue1817()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -877,6 +904,7 @@ public void Test_Issue1817()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1630
public void Test_Issue1630()
{
var result = CompilationHelper.Compile(@"
Expand All @@ -901,6 +929,7 @@ public void Test_Issue1630()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1627
public void Test_Issue1627()
{
var result = CompilationHelper.Compile(("main.bicep", @"
Expand Down Expand Up @@ -929,6 +958,7 @@ param foo int
}

[TestMethod]
// https://github.com/azure/bicep/issues/1941
public void Test_Issue1941()
{
var result = CompilationHelper.Compile(@"
Expand Down Expand Up @@ -993,6 +1023,7 @@ param location string
}

[TestMethod]
// https://github.com/azure/bicep/issues/657
public void Test_Issue657_discriminators()
{
var customTypes = new [] {
Expand Down Expand Up @@ -1090,6 +1121,7 @@ public void Test_Issue657_discriminators()
}

[TestMethod]
// https://github.com/azure/bicep/issues/657
public void Test_Issue657_enum()
{
var customTypes = new [] {
Expand Down Expand Up @@ -1178,6 +1210,7 @@ public void Test_Issue657_enum()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1985
public void Test_Issue1985()
{
var result = CompilationHelper.Compile(@"
Expand Down Expand Up @@ -1212,6 +1245,7 @@ public void Test_Issue1985()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1986
public void Test_Issue1986()
{
var result = CompilationHelper.Compile(@"
Expand Down Expand Up @@ -1252,6 +1286,7 @@ public void Test_Issue1986()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1986
public void Test_Issue1986_nested()
{
var result = CompilationHelper.Compile(@"
Expand Down Expand Up @@ -1291,6 +1326,7 @@ public void Test_Issue1986_nested()
}

[TestMethod]
// https://github.com/azure/bicep/issues/1986
public void Test_Issue1986_loops()
{
var result = CompilationHelper.Compile(@"
Expand Down Expand Up @@ -1333,5 +1369,58 @@ public void Test_Issue1986_loops()
"[resourceId('Microsoft.Network/virtualNetworks', variables('vnets')[copyIndex()])]", // dependsOn should include the virtualNetwork parent resource
});
}

[TestMethod]
// https://github.com/azure/bicep/issues/1993
public void Test_Issue1993()
{
var result = CompilationHelper.Compile(@"
//""flat"" string
var jsonStringFlat = '[""one"",""two"",""three"" ]'
//Good Array
var jsonStringGood = '''
[
""one"",
""two"",
""three""
]'''
//Bad Array
var jsonStringBad = '''
[
""one"",
""two"",
""three""
]
'''
var jsonArrayFlat = json(jsonStringFlat)
var jsonArrayGood = json(jsonStringGood)
var jsonArrayBad = json(jsonStringBad)
output flatArray array = [for (name, i) in jsonArrayFlat: {
element: name
}]
output goodArray array = [for (name, i) in jsonArrayGood: {
element: name
}]
output badArray array = [for (name, i) in jsonArrayBad : {
element: name
}]
");


var evaluated = TemplateEvaluator.Evaluate(result.Template);
var expectedOutput = new JArray {
new JObject { ["element"] = "one" },
new JObject { ["element"] = "two" },
new JObject { ["element"] = "three" },
};
evaluated.Should().HaveValueAtPath("$.outputs['flatArray'].value", expectedOutput);
evaluated.Should().HaveValueAtPath("$.outputs['goodArray'].value", expectedOutput);
evaluated.Should().HaveValueAtPath("$.outputs['badArray'].value", expectedOutput);
}
}
}
4 changes: 2 additions & 2 deletions src/Bicep.Core.Samples/Files/Variables_LF/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"_generator": {
"name": "bicep",
"version": "dev",
"templateHash": "4515800226062018461"
"templateHash": "5466344590259907996"
}
},
"parameters": {
Expand Down Expand Up @@ -34,7 +34,7 @@
"doubleInterp": "[format('abc{0}_{1}', format('def{0}', 123), format('{0}{1}', 456, 789))]",
"curliesInInterp": "[format('{{{0}{{0}}{1}}}', 123, true())]",
"bracketInTheMiddle": "a[b]",
"bracketAtBeginning": "[[test",
"bracketAtBeginning": "[test",
"enclosingBrackets": "[[test]",
"emptyJsonArray": "[[]",
"interpolatedBrackets": "[format('[{0}]', variables('myInt'))]",
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core/Bicep.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="Azure.Deployments.Expression" Version="1.0.162" />
<PackageReference Include="Azure.Deployments.Expression" Version="1.0.168" />
<PackageReference Include="Azure.Deployments.Core" Version="1.0.168" />
<PackageReference Include="Azure.Bicep.Types" Version="0.1.95" />
<PackageReference Include="Azure.Bicep.Types.Az" Version="0.1.95" />
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Decompiler/Bicep.Decompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="Azure.Deployments.Expression" Version="1.0.162" />
<PackageReference Include="Azure.Deployments.Expression" Version="1.0.168" />
</ItemGroup>

</Project>

0 comments on commit 8871afd

Please sign in to comment.