Skip to content

Commit 4653664

Browse files
authored
Fixes expand issue with JObject key Azure#2751 (Azure#2769)
1 parent 316dc93 commit 4653664

File tree

8 files changed

+623
-1
lines changed

8 files changed

+623
-1
lines changed

docs/CHANGELOG-v1.md

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ What's changed since pre-release v1.35.0-B0030:
4141
- General improvements:
4242
- Quality updates to rule documentation by @BernieWhite.
4343
[#2570](https://github.com/Azure/PSRule.Rules.Azure/issues/2570)
44+
- Bug fixes:
45+
- Fixed failed to expand JObject value with invalid key by @BernieWhite.
46+
[#2751](https://github.com/Azure/PSRule.Rules.Azure/issues/2751)
4447

4548
## v1.35.0-B0030 (pre-release)
4649

src/PSRule.Rules.Azure/Data/Template/Mocks.cs

+15
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ public virtual JToken GetValue(TypePrimitive type)
318318

319319
public virtual JToken GetValue(object key)
320320
{
321+
key = GetBaseObject(key);
321322
var result = base[key];
322323
if (result == null)
323324
{
@@ -511,5 +512,19 @@ private static bool TryExpandId(JObject o)
511512
}
512513
return false;
513514
}
515+
516+
/// <summary>
517+
/// Unwrap the base object from a JToken.
518+
/// </summary>
519+
private static object GetBaseObject(object o)
520+
{
521+
if (o is JToken t_string && t_string.Type == JTokenType.String)
522+
return t_string.Value<string>();
523+
524+
if (o is JToken t_int && t_int.Type == JTokenType.Integer)
525+
return t_int.Value<int>();
526+
527+
return o;
528+
}
514529
}
515530
}

src/PSRule.Rules.Azure/Data/Template/TemplateVisitor.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using PSRule.Rules.Azure.Configuration;
1212
using PSRule.Rules.Azure.Pipeline;
1313
using PSRule.Rules.Azure.Resources;
14+
using static PSRule.Rules.Azure.Data.Template.Mock;
1415

1516
namespace PSRule.Rules.Azure.Data.Template
1617
{
@@ -910,7 +911,7 @@ public LazyOutput(TemplateContext context, string name, JObject value)
910911
public object GetValue()
911912
{
912913
ResolveProperty(_Context, _Value, PROPERTY_VALUE);
913-
return _Value;
914+
return new MockObject(_Value);
914915
}
915916
}
916917

tests/PSRule.Rules.Azure.Tests/PSRule.Rules.Azure.Tests.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@
236236
<None Update="Tests.Bicep.34.json">
237237
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
238238
</None>
239+
<None Update="Tests.Bicep.35.json">
240+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
241+
</None>
239242
<None Update="Tests.Bicep.4.json">
240243
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
241244
</None>

tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace PSRule.Rules.Azure
1515
{
16+
/// <summary>
17+
/// This class tests the functionality of the <see cref="TemplateVisitor"/> class that is used to expand resources from an ARM template/ Bicep file.
18+
/// </summary>
1619
public sealed class TemplateVisitorTests
1720
{
1821
[Fact]
@@ -1092,6 +1095,15 @@ public void UnionMockWithArray()
10921095
Assert.Equal("Microsoft.KeyVault/vaults/accessPolicies", actual["type"].Value<string>());
10931096
}
10941097

1098+
/// <summary>
1099+
/// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2751.
1100+
/// </summary>
1101+
[Fact]
1102+
public void Index_into_mock_output_object()
1103+
{
1104+
var resources = ProcessTemplate(GetSourcePath("Tests.Bicep.35.json"), null, out _);
1105+
}
1106+
10951107
#region Helper methods
10961108

10971109
private static string GetSourcePath(string fileName)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2751.
5+
6+
var roles = ['Reader']
7+
8+
module script 'br/public:avm/res/resources/deployment-script:0.1.2' = {
9+
name: 'script'
10+
params: {
11+
kind: 'AzurePowerShell'
12+
name: toLower('ds-001')
13+
retentionInterval: 'PT1H'
14+
azPowerShellVersion: '9.7'
15+
arguments: ''
16+
scriptContent: '''
17+
18+
'''
19+
}
20+
}
21+
22+
module other './Tests.Bicep.35.child.bicep' = [
23+
for role in roles: {
24+
name: role
25+
params: {
26+
value: script.outputs.outputs[role]
27+
}
28+
}
29+
]
30+
31+
output other string = other[0].outputs.value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
param value string
5+
6+
output value string = value

0 commit comments

Comments
 (0)