diff --git a/docs/CHANGELOG-v1.md b/docs/CHANGELOG-v1.md index ac4432daff6..f803b50abe3 100644 --- a/docs/CHANGELOG-v1.md +++ b/docs/CHANGELOG-v1.md @@ -32,6 +32,12 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers ## Unreleased +What's changed since v1.35.1: + +- Bug fixes: + - Fixed regression when handing ambiguous mock array outputs by @BernieWhite. + [#2801](https://github.com/Azure/PSRule.Rules.Azure/issues/2801) + ## v1.35.1 What's changed since v1.35.0: diff --git a/src/PSRule.Rules.Azure/Data/Template/Mocks.cs b/src/PSRule.Rules.Azure/Data/Template/Mocks.cs index d71d6eae5d0..d54ea7f6d01 100644 --- a/src/PSRule.Rules.Azure/Data/Template/Mocks.cs +++ b/src/PSRule.Rules.Azure/Data/Template/Mocks.cs @@ -215,7 +215,7 @@ public TValue GetValue() public JToken GetValue(TypePrimitive type) { - return type == TypePrimitive.Array ? this : null; + return type == TypePrimitive.None || type == TypePrimitive.Array ? this : null; } public JToken GetValue(object key) diff --git a/tests/PSRule.Rules.Azure.Tests/PSRule.Rules.Azure.Tests.csproj b/tests/PSRule.Rules.Azure.Tests/PSRule.Rules.Azure.Tests.csproj index f5ecb7c1632..6dc83d388ca 100644 --- a/tests/PSRule.Rules.Azure.Tests/PSRule.Rules.Azure.Tests.csproj +++ b/tests/PSRule.Rules.Azure.Tests/PSRule.Rules.Azure.Tests.csproj @@ -239,6 +239,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs b/tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs index 1e1337c4099..43cdb0d75f7 100644 --- a/tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs +++ b/tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs @@ -1101,7 +1101,7 @@ public void UnionMockWithArray() [Fact] public void ProcessTemplate_WhenIndexIntoMock_ShouldReturnMock() { - var resources = ProcessTemplate(GetSourcePath("Tests.Bicep.35.json"), null, out _); + ProcessTemplate(GetSourcePath("Tests.Bicep.35.json"), null, out _); } /// @@ -1116,6 +1116,23 @@ public void ProcessTemplate_WhenParameterNullWithDefault_ShouldUseDefault() Assert.Equal("Standard_LRS", actual["sku"]["name"].Value()); } + /// + /// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2801. + /// + [Fact] + public void ProcessTemplate_WhenMockArrayOutputAndTypeIsUnknown_ShouldReturnArray() + { + ProcessTemplate(GetSourcePath("Tests.Bicep.36.json"), null, out var templateContext); + + Assert.True(templateContext.RootDeployment.TryOutput("items", out JObject result)); + var items = result["value"][0]["items"].Value(); + + Assert.Single(items); + var actual = items[0].Value(); + Assert.Equal("name1", actual["name"].Value()); + Assert.Equal("value1", actual["value"].Value()); + } + #region Helper methods private static string GetSourcePath(string fileName) diff --git a/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.bicep b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.bicep new file mode 100644 index 00000000000..587222691ad --- /dev/null +++ b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.bicep @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2801. + +module child 'Tests.Bicep.36.child.bicep' = { + name: 'child' +} + +output items array = [ + { + items: child.outputs.items + } +] diff --git a/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.child.bicep b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.child.bicep new file mode 100644 index 00000000000..1840b7d688a --- /dev/null +++ b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.child.bicep @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +output items array = concat( + [], + [ + { + name: 'name1' + value: 'value1' + } + ] +) diff --git a/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.json b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.json new file mode 100644 index 00000000000..5cb1b243059 --- /dev/null +++ b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.json @@ -0,0 +1,52 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "metadata": { + "_generator": { + "name": "bicep", + "version": "0.26.54.24096", + "templateHash": "12279334207887669240" + } + }, + "resources": [ + { + "type": "Microsoft.Resources/deployments", + "apiVersion": "2022-09-01", + "name": "child", + "properties": { + "expressionEvaluationOptions": { + "scope": "inner" + }, + "mode": "Incremental", + "template": { + "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "metadata": { + "_generator": { + "name": "bicep", + "version": "0.26.54.24096", + "templateHash": "14837468981850150943" + } + }, + "resources": [], + "outputs": { + "items": { + "type": "array", + "value": "[concat(createArray(), createArray(createObject('name', 'name1', 'value', 'value1')))]" + } + } + } + } + } + ], + "outputs": { + "items": { + "type": "array", + "value": [ + { + "items": "[reference(resourceId('Microsoft.Resources/deployments', 'child'), '2022-09-01').outputs.items.value]" + } + ] + } + } +} \ No newline at end of file