Skip to content

Commit 0997c93

Browse files
authored
[Resources] Handle nested array changes in What-If formatter (#20689)
* Handle nested array changes * Update changelog
1 parent 664e09d commit 0997c93

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

src/Resources/ResourceManager/Formatters/WhatIfOperationResultFormatter.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private void FormatPropertyChange(PSWhatIfPropertyChange propertyChange, int max
330330

331331
case PropertyChangeType.Array:
332332
this.FormatPropertyChangePath(propertyChangeType, path, null, children, maxPathLength, indentLevel);
333-
this.FormatPropertyArrayChange(propertyChange.Children, indentLevel + 1);
333+
this.FormatPropertyArrayChange(propertyChange, propertyChange.Children, indentLevel + 1);
334334
break;
335335

336336
case PropertyChangeType.NoEffect:
@@ -351,6 +351,11 @@ private void FormatPropertyChangePath(
351351
int maxPathLength,
352352
int indentLevel)
353353
{
354+
if (string.IsNullOrEmpty(path))
355+
{
356+
return;
357+
}
358+
354359
int paddingWidth = maxPathLength - path.Length + 1;
355360
bool hasChildren = children != null && children.Count > 0;
356361

@@ -449,8 +454,17 @@ private void FormatPropertyModify(PSWhatIfPropertyChange propertyChange, int ind
449454
}
450455
}
451456

452-
private void FormatPropertyArrayChange(IList<PSWhatIfPropertyChange> propertyChanges, int indentLevel)
457+
private void FormatPropertyArrayChange(PSWhatIfPropertyChange parentPropertyChange, IList<PSWhatIfPropertyChange> propertyChanges, int indentLevel)
453458
{
459+
if (string.IsNullOrEmpty(parentPropertyChange.Path))
460+
{
461+
// The parent change doesn't have a path, which means the current
462+
// array change is a nested change. We need to decrease indent_level
463+
// and print indentation before printing "[".
464+
indentLevel--;
465+
FormatIndent(indentLevel);
466+
}
467+
454468
if (propertyChanges.Count == 0)
455469
{
456470
this.Builder.AppendLine("[]");

src/Resources/Resources.Test/Formatters/WhatIfOperationResultFormatterTests.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Microsoft.Azure.Commands.Resources.Test.Formatters
1919
using ResourceManager.Cmdlets.SdkModels.Deployments;
2020
using System;
2121
using System.Collections.Generic;
22+
using System.IO;
2223
using WindowsAzure.Commands.ScenarioTest;
2324
using Xunit;
2425

@@ -441,6 +442,104 @@ public void Format_ChangeTypeModify_FormatsPropertyChanges()
441442
// Assert.
442443
Assert.Contains(expected, result);
443444
}
445+
446+
[Fact]
447+
[Trait(Category.AcceptanceType, Category.CheckIn)]
448+
public void Format_nested_array_changes_does_not_throw()
449+
{
450+
// Arrange.
451+
var whatIfChanges = new List<WhatIfChange>
452+
{
453+
new WhatIfChange
454+
{
455+
ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000004/resourceGroups/rg4/providers/Microsoft.DocumentDB/databaseAccounts/myaccount/sqlDatabases/accesscontrol/containers/workflows",
456+
ChangeType = ChangeType.Modify,
457+
Delta = new List<WhatIfPropertyChange>
458+
{
459+
new WhatIfPropertyChange
460+
{
461+
Path = "properties.resource.indexingPolicy.compositeIndexes",
462+
PropertyChangeType = PropertyChangeType.Array,
463+
Children = new List<WhatIfPropertyChange>
464+
{
465+
new WhatIfPropertyChange
466+
{
467+
Path = "0",
468+
PropertyChangeType = PropertyChangeType.Modify,
469+
Children = new List<WhatIfPropertyChange>
470+
{
471+
new WhatIfPropertyChange
472+
{
473+
Path = null,
474+
PropertyChangeType = PropertyChangeType.Array,
475+
Children = new List<WhatIfPropertyChange>
476+
{
477+
new WhatIfPropertyChange
478+
{
479+
Path = "0",
480+
PropertyChangeType = PropertyChangeType.Modify,
481+
Children = new List<WhatIfPropertyChange>
482+
{
483+
new WhatIfPropertyChange
484+
{
485+
Path = "order",
486+
PropertyChangeType = PropertyChangeType.Delete,
487+
Before = "ascending",
488+
}
489+
}
490+
},
491+
new WhatIfPropertyChange
492+
{
493+
Path = "1",
494+
PropertyChangeType = PropertyChangeType.Modify,
495+
Children = new List<WhatIfPropertyChange>
496+
{
497+
new WhatIfPropertyChange
498+
{
499+
Path = "order",
500+
PropertyChangeType = PropertyChangeType.Delete,
501+
Before = "ascending",
502+
}
503+
}
504+
}
505+
}
506+
}
507+
}
508+
}
509+
}
510+
},
511+
}
512+
}
513+
};
514+
515+
string expected = $@"
516+
Scope: /subscriptions/00000000-0000-0000-0000-000000000004/resourceGroups/rg4
517+
{Color.Purple}
518+
~ Microsoft.DocumentDB/databaseAccounts/myaccount/sqlDatabases/accesscontrol/containers/workflows{Color.Reset}
519+
{Color.Purple}~{Color.Reset} properties.resource.indexingPolicy.compositeIndexes{Color.Reset}:{Color.Reset} [
520+
{Color.Purple}~{Color.Reset} 0{Color.Reset}:{Color.Reset}
521+
522+
[
523+
{Color.Purple}~{Color.Reset} 0{Color.Reset}:{Color.Reset}
524+
525+
{Color.Orange}-{Color.Reset} order{Color.Reset}:{Color.Reset} {Color.Orange}""ascending""{Color.Reset}
526+
527+
{Color.Purple}~{Color.Reset} 1{Color.Reset}:{Color.Reset}
528+
529+
{Color.Orange}-{Color.Reset} order{Color.Reset}:{Color.Reset} {Color.Orange}""ascending""{Color.Reset}
530+
531+
]
532+
533+
]
534+
".Replace("\r\n", Environment.NewLine);
535+
536+
// Act.
537+
string result = WhatIfOperationResultFormatter.Format(
538+
new PSWhatIfOperationResult(new WhatIfOperationResult(changes: whatIfChanges)));
539+
540+
// Assert.
541+
Assert.Contains(expected, result);
542+
}
444543
}
445544
}
446545

src/Resources/Resources/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Fixed an issue where running deployment cmdlets with `-WhatIf` throws exception when formatting results with nested array changes
2223

2324
## Version 6.5.1
2425
* Fixed issue introduced in previous fix for `Set-AzPolicySetDefinition` InternalServerError when the initiative is too large [#20238], which will remove space in value.

0 commit comments

Comments
 (0)