From 908baf059b5be2d35e8fd3aabbd37ecad76ecdb3 Mon Sep 17 00:00:00 2001 From: Bernie White Date: Wed, 3 Apr 2024 10:51:26 +1000 Subject: [PATCH] Fixes null parameter overrides default #2795 (#2797) --- docs/CHANGELOG-v1.md | 6 ++ .../Data/Template/TemplateVisitor.cs | 3 + .../TemplateVisitorTests.cs | 14 ++++- .../Tests.Bicep.27.bicep | 3 + .../Tests.Bicep.27.child.bicep | 59 +++++++++++-------- .../Tests.Bicep.27.json | 19 ++++-- 6 files changed, 72 insertions(+), 32 deletions(-) diff --git a/docs/CHANGELOG-v1.md b/docs/CHANGELOG-v1.md index 9af2dd0e32e..b3501c64cc9 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.0: + +- Bug fixes: + - Fixed null parameter overrides default value by @BernieWhite. + [#2795](https://github.com/Azure/PSRule.Rules.Azure/issues/2795) + ## v1.35.0 What's changed since v1.34.2: diff --git a/src/PSRule.Rules.Azure/Data/Template/TemplateVisitor.cs b/src/PSRule.Rules.Azure/Data/Template/TemplateVisitor.cs index a8298b877b9..362f6931486 100644 --- a/src/PSRule.Rules.Azure/Data/Template/TemplateVisitor.cs +++ b/src/PSRule.Rules.Azure/Data/Template/TemplateVisitor.cs @@ -352,6 +352,9 @@ private bool AssignParameterLiteral(string name, JObject parameter) private void AddParameterAssignment(string name, JToken value) { + if (value == null || value.Type == JTokenType.Null || value.Type == JTokenType.Undefined) + return; + _ParameterAssignments.Add(name, value); } diff --git a/tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs b/tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs index 640bf73b59d..1e1337c4099 100644 --- a/tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs +++ b/tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs @@ -1099,11 +1099,23 @@ public void UnionMockWithArray() /// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2751. /// [Fact] - public void Index_into_mock_output_object() + public void ProcessTemplate_WhenIndexIntoMock_ShouldReturnMock() { var resources = ProcessTemplate(GetSourcePath("Tests.Bicep.35.json"), null, out _); } + /// + /// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2795. + /// + [Fact] + public void ProcessTemplate_WhenParameterNullWithDefault_ShouldUseDefault() + { + var resources = ProcessTemplate(GetSourcePath("Tests.Bicep.27.json"), null, out _); + + var actual = resources.FirstOrDefault(r => r["type"].Value() == "Microsoft.Storage/storageAccounts"); + Assert.Equal("Standard_LRS", actual["sku"]["name"].Value()); + } + #region Helper methods private static string GetSourcePath(string fileName) diff --git a/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.bicep b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.bicep index acdfcfeede3..a1daec2e085 100644 --- a/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.bicep +++ b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.bicep @@ -3,6 +3,9 @@ module child 'Tests.Bicep.27.child.bicep' = { name: 'child' + params: { + skuName: null + } } output childFromFor string = child.outputs.fromFor diff --git a/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.child.bicep b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.child.bicep index 83f116ea3cf..0f88775719c 100644 --- a/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.child.bicep +++ b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.child.bicep @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +param skuName string = 'Standard_LRS' param minTLSVersion string? param corsRules corsRule @@ -17,11 +18,13 @@ param accessPolicies array? @secure() param secrets object? -var formattedAccessPolicies = [for accessPolicy in (accessPolicies ?? []): { - objectId: accessPolicy.objectId - tenantId: contains(accessPolicy, 'tenantId') ? accessPolicy.tenantId : tenant().tenantId - permissions: {} -}] +var formattedAccessPolicies = [ + for accessPolicy in (accessPolicies ?? []): { + objectId: accessPolicy.objectId + tenantId: contains(accessPolicy, 'tenantId') ? accessPolicy.tenantId : tenant().tenantId + permissions: {} + } +] var secretList = secrets.?secureList ?? [] @@ -30,7 +33,7 @@ resource storage 'Microsoft.Storage/storageAccounts@2023-01-01' = { #disable-next-line no-loc-expr-outside-params location: resourceGroup().location sku: { - name: 'Standard_LRS' + name: skuName } kind: 'StorageV2' properties: { @@ -61,12 +64,14 @@ resource kv 'Microsoft.KeyVault/vaults@2023-07-01' = { } } -resource kvSecret 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = [for item in secretList: { - name: item.name - properties: { - value: item.value +resource kvSecret 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = [ + for item in secretList: { + name: item.name + properties: { + value: item.value + } } -}] +] resource storageAccount_objectReplicationPolicy 'Microsoft.Storage/storageAccounts/objectReplicationPolicies@2022-09-01' = { name: 'default' @@ -85,22 +90,24 @@ resource storageAccount_objectReplicationPolicy 'Microsoft.Storage/storageAccoun } } -resource storageAccount_objectReplicationPolicyItems 'Microsoft.Storage/storageAccounts/objectReplicationPolicies@2022-09-01' = [for (item, index) in [ 1 ]: { - name: 'default${index}' - parent: storage - properties: { - sourceAccount: 'sourceId' - destinationAccount: 'destId' - rules: [ - { - ruleId: null - sourceContainer: 'source' - destinationContainer: 'dest' - filters: null - } - ] +resource storageAccount_objectReplicationPolicyItems 'Microsoft.Storage/storageAccounts/objectReplicationPolicies@2022-09-01' = [ + for (item, index) in [1]: { + name: 'default${index}' + parent: storage + properties: { + sourceAccount: 'sourceId' + destinationAccount: 'destId' + rules: [ + { + ruleId: null + sourceContainer: 'source' + destinationContainer: 'dest' + filters: null + } + ] + } } -}] +] output policyId string = storageAccount_objectReplicationPolicy.properties.policyId output ruleIds string[] = map(storageAccount_objectReplicationPolicy.properties.rules, rule => rule.ruleId) diff --git a/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.json b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.json index 5419d54d8bb..c1207a8352e 100644 --- a/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.json +++ b/tests/PSRule.Rules.Azure.Tests/Tests.Bicep.27.json @@ -4,8 +4,8 @@ "metadata": { "_generator": { "name": "bicep", - "version": "0.23.1.45101", - "templateHash": "1063354008601109842" + "version": "0.26.54.24096", + "templateHash": "7737727980270162371" } }, "resources": [ @@ -18,6 +18,11 @@ "scope": "inner" }, "mode": "Incremental", + "parameters": { + "skuName": { + "value": null + } + }, "template": { "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "languageVersion": "2.0", @@ -25,8 +30,8 @@ "metadata": { "_generator": { "name": "bicep", - "version": "0.23.1.45101", - "templateHash": "11238883118622659404" + "version": "0.26.54.24096", + "templateHash": "14665743710501821887" } }, "definitions": { @@ -68,6 +73,10 @@ } }, "parameters": { + "skuName": { + "type": "string", + "defaultValue": "Standard_LRS" + }, "minTLSVersion": { "type": "string", "nullable": true @@ -118,7 +127,7 @@ "name": "test", "location": "[resourceGroup().location]", "sku": { - "name": "Standard_LRS" + "name": "[parameters('skuName')]" }, "kind": "StorageV2", "properties": {