diff --git a/src/Resources/ResourceManager/Formatters/WhatIfOperationResultFormatter.cs b/src/Resources/ResourceManager/Formatters/WhatIfOperationResultFormatter.cs index 501973aecdf1..5037740378f7 100644 --- a/src/Resources/ResourceManager/Formatters/WhatIfOperationResultFormatter.cs +++ b/src/Resources/ResourceManager/Formatters/WhatIfOperationResultFormatter.cs @@ -330,7 +330,7 @@ private void FormatPropertyChange(PSWhatIfPropertyChange propertyChange, int max case PropertyChangeType.Array: this.FormatPropertyChangePath(propertyChangeType, path, null, children, maxPathLength, indentLevel); - this.FormatPropertyArrayChange(propertyChange.Children, indentLevel + 1); + this.FormatPropertyArrayChange(propertyChange, propertyChange.Children, indentLevel + 1); break; case PropertyChangeType.NoEffect: @@ -351,6 +351,11 @@ private void FormatPropertyChangePath( int maxPathLength, int indentLevel) { + if (string.IsNullOrEmpty(path)) + { + return; + } + int paddingWidth = maxPathLength - path.Length + 1; bool hasChildren = children != null && children.Count > 0; @@ -449,8 +454,17 @@ private void FormatPropertyModify(PSWhatIfPropertyChange propertyChange, int ind } } - private void FormatPropertyArrayChange(IList propertyChanges, int indentLevel) + private void FormatPropertyArrayChange(PSWhatIfPropertyChange parentPropertyChange, IList propertyChanges, int indentLevel) { + if (string.IsNullOrEmpty(parentPropertyChange.Path)) + { + // The parent change doesn't have a path, which means the current + // array change is a nested change. We need to decrease indent_level + // and print indentation before printing "[". + indentLevel--; + FormatIndent(indentLevel); + } + if (propertyChanges.Count == 0) { this.Builder.AppendLine("[]"); diff --git a/src/Resources/Resources.Test/Formatters/WhatIfOperationResultFormatterTests.cs b/src/Resources/Resources.Test/Formatters/WhatIfOperationResultFormatterTests.cs index 2576fb29484c..de9ebca25b34 100644 --- a/src/Resources/Resources.Test/Formatters/WhatIfOperationResultFormatterTests.cs +++ b/src/Resources/Resources.Test/Formatters/WhatIfOperationResultFormatterTests.cs @@ -19,6 +19,7 @@ namespace Microsoft.Azure.Commands.Resources.Test.Formatters using ResourceManager.Cmdlets.SdkModels.Deployments; using System; using System.Collections.Generic; + using System.IO; using WindowsAzure.Commands.ScenarioTest; using Xunit; @@ -441,6 +442,104 @@ public void Format_ChangeTypeModify_FormatsPropertyChanges() // Assert. Assert.Contains(expected, result); } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void Format_nested_array_changes_does_not_throw() + { + // Arrange. + var whatIfChanges = new List + { + new WhatIfChange + { + ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000004/resourceGroups/rg4/providers/Microsoft.DocumentDB/databaseAccounts/myaccount/sqlDatabases/accesscontrol/containers/workflows", + ChangeType = ChangeType.Modify, + Delta = new List + { + new WhatIfPropertyChange + { + Path = "properties.resource.indexingPolicy.compositeIndexes", + PropertyChangeType = PropertyChangeType.Array, + Children = new List + { + new WhatIfPropertyChange + { + Path = "0", + PropertyChangeType = PropertyChangeType.Modify, + Children = new List + { + new WhatIfPropertyChange + { + Path = null, + PropertyChangeType = PropertyChangeType.Array, + Children = new List + { + new WhatIfPropertyChange + { + Path = "0", + PropertyChangeType = PropertyChangeType.Modify, + Children = new List + { + new WhatIfPropertyChange + { + Path = "order", + PropertyChangeType = PropertyChangeType.Delete, + Before = "ascending", + } + } + }, + new WhatIfPropertyChange + { + Path = "1", + PropertyChangeType = PropertyChangeType.Modify, + Children = new List + { + new WhatIfPropertyChange + { + Path = "order", + PropertyChangeType = PropertyChangeType.Delete, + Before = "ascending", + } + } + } + } + } + } + } + } + }, + } + } + }; + + string expected = $@" +Scope: /subscriptions/00000000-0000-0000-0000-000000000004/resourceGroups/rg4 +{Color.Purple} + ~ Microsoft.DocumentDB/databaseAccounts/myaccount/sqlDatabases/accesscontrol/containers/workflows{Color.Reset} + {Color.Purple}~{Color.Reset} properties.resource.indexingPolicy.compositeIndexes{Color.Reset}:{Color.Reset} [ + {Color.Purple}~{Color.Reset} 0{Color.Reset}:{Color.Reset} + + [ + {Color.Purple}~{Color.Reset} 0{Color.Reset}:{Color.Reset} + + {Color.Orange}-{Color.Reset} order{Color.Reset}:{Color.Reset} {Color.Orange}""ascending""{Color.Reset} + + {Color.Purple}~{Color.Reset} 1{Color.Reset}:{Color.Reset} + + {Color.Orange}-{Color.Reset} order{Color.Reset}:{Color.Reset} {Color.Orange}""ascending""{Color.Reset} + + ] + + ] +".Replace("\r\n", Environment.NewLine); + + // Act. + string result = WhatIfOperationResultFormatter.Format( + new PSWhatIfOperationResult(new WhatIfOperationResult(changes: whatIfChanges))); + + // Assert. + Assert.Contains(expected, result); + } } } diff --git a/src/Resources/Resources/ChangeLog.md b/src/Resources/Resources/ChangeLog.md index 99374ea919b1..da394d9a2902 100644 --- a/src/Resources/Resources/ChangeLog.md +++ b/src/Resources/Resources/ChangeLog.md @@ -19,6 +19,7 @@ --> ## Upcoming Release +* Fixed an issue where running deployment cmdlets with `-WhatIf` throws exception when formatting results with nested array changes ## Version 6.5.1 * Fixed issue introduced in previous fix for `Set-AzPolicySetDefinition` InternalServerError when the initiative is too large [#20238], which will remove space in value.