Skip to content

Commit 4929e40

Browse files
authored
[Az Resources] Fix resource tags casing serialization - tags casing not being preserved when passed as parameters (#15920)
* fix tags params * update changelog * fix unit test
1 parent c3a34f7 commit 4929e40

File tree

9 files changed

+2953
-12
lines changed

9 files changed

+2953
-12
lines changed

src/Resources/ResourceManager/Json/CamelCasePropertyNamesWithOverridesContractResolver.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,12 @@ protected override JsonDictionaryContract CreateDictionaryContract(Type objectTy
5454
{
5555
var contract = base.CreateDictionaryContract(objectType);
5656

57-
var attributes = objectType.GetCustomAttributes(attributeType: typeof(JsonPreserveCaseDictionaryAttribute), inherit: true);
58-
if (attributes.Any())
59-
{
60-
// TODO: Remove IfDef code
57+
// TODO: Remove IfDef code
6158
#if NETSTANDARD
62-
contract.DictionaryKeyResolver = keyName => keyName;
59+
contract.DictionaryKeyResolver = keyName => keyName;
6360
#else
64-
contract.PropertyNameResolver = propertyName => propertyName;
61+
contract.PropertyNameResolver = propertyName => propertyName;
6562
#endif
66-
}
67-
6863
return contract;
6964
}
7065
}

src/Resources/Resources.Test/Json/PSJsonSerializerTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,10 @@ public void Serialize_Hashtable_Success()
9393

9494
JToken parsedResult = result.FromJson<JToken>();
9595

96-
// NOTE(jcotillo): JsonExtensions is now camelCasing all property keys
97-
// therefore even though Bar was set as PascalCase, after serializing it
98-
// the key became camelCase.
9996
JToken expected = JToken.FromObject(new
10097
{
10198
foo = "fooValue",
102-
bar = true,
99+
Bar = true,
103100
nested = new
104101
{
105102
foo = "4d44fe86-f04a-4ba5-9900-abdec8cb11c1",

src/Resources/Resources.Test/ScenarioTests/DeploymentTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,19 @@ public void TestNewDeploymentFromTemplateAndParameterFileContainingDatetimeOutpu
216216
{
217217
TestRunner.RunTestScript("Test-NewDeploymentFromTemplateAndParameterFileContainingDatetimeOutput");
218218
}
219+
220+
[Fact]
221+
[Trait(Category.AcceptanceType, Category.CheckIn)]
222+
public void TestNewDeploymentFromTemplateFileContainingTagsOutput()
223+
{
224+
TestRunner.RunTestScript("Test-NewDeploymentFromTemplateFileContainingTagsOutput");
225+
}
226+
227+
[Fact]
228+
[Trait(Category.AcceptanceType, Category.CheckIn)]
229+
public void TestNewDeploymentFromTemplateAndParameterFileContainingTagsOutput()
230+
{
231+
TestRunner.RunTestScript("Test-NewDeploymentFromTemplateAndParameterFileContainingTagsOutput");
232+
}
219233
}
220234
}

src/Resources/Resources.Test/ScenarioTests/DeploymentTests.ps1

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,116 @@ function Test-NewDeploymentFromTemplateAndParameterFileContainingDatetimeOutput
10041004
Assert-AreEqual $datetimeFormatted $datetimeOutput
10051005
}
10061006

1007+
finally
1008+
{
1009+
# Cleanup
1010+
Clean-ResourceGroup $rgname
1011+
}
1012+
}
1013+
1014+
<#
1015+
.SYNOPSIS
1016+
Tests deployment via template and parameter file containing a tags with different casing.
1017+
#>
1018+
function Test-NewDeploymentFromTemplateFileContainingTagsOutput
1019+
{
1020+
# Setup
1021+
$rgname = Get-ResourceGroupName
1022+
$rname = Get-ResourceName
1023+
$rglocation = "West US 2"
1024+
1025+
try
1026+
{
1027+
# Test
1028+
New-AzResourceGroup -Name $rgname -Location $rglocation
1029+
1030+
$tagsToCompare = @{
1031+
"MY_FIRST_TAG" = "tagValue";
1032+
"MYFIRSTTAG" = "tagvalue2";
1033+
"mysecondTag" = "tagValue3";
1034+
"mythirdtag" = "tagvalue4"
1035+
}
1036+
1037+
$parameters = @{ "tags"= $tagsToCompare }
1038+
1039+
$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile simpleTemplateWithTagsOutput.json -TemplateParameterObject $parameters
1040+
1041+
# Assert
1042+
Assert-AreEqual Succeeded $deployment.ProvisioningState
1043+
1044+
$subId = (Get-AzContext).Subscription.SubscriptionId
1045+
$deploymentId = "/subscriptions/$subId/resourcegroups/$rgname/providers/Microsoft.Resources/deployments/$rname"
1046+
$getById = Get-AzResourceGroupDeployment -Id $deploymentId
1047+
Assert-AreEqual $getById.DeploymentName $deployment.DeploymentName
1048+
1049+
$tagsOutput = $getById.Outputs.tags.Value
1050+
1051+
$tagsOutputJson = ConvertTo-Json -Compress $tagsOutput
1052+
1053+
# Performs a case sensitive comparison
1054+
# Doing a foreach on the keys and comparing against the JSON string
1055+
# this needed because the flag -AsHashTable doesn't seem to work
1056+
# in ConvertTo-Json cmdlet
1057+
foreach ($tag in $tagsToCompare.Keys)
1058+
{
1059+
Assert-True { $tagsOutputJson -clike "*${tag}*"}
1060+
}
1061+
}
1062+
1063+
finally
1064+
{
1065+
# Cleanup
1066+
Clean-ResourceGroup $rgname
1067+
}
1068+
}
1069+
1070+
<#
1071+
.SYNOPSIS
1072+
Tests deployment via template and parameter file containing a tags with different casing.
1073+
#>
1074+
function Test-NewDeploymentFromTemplateAndParameterFileContainingTagsOutput
1075+
{
1076+
# Setup
1077+
$rgname = Get-ResourceGroupName
1078+
$rname = Get-ResourceName
1079+
$rglocation = "West US 2"
1080+
1081+
try
1082+
{
1083+
# Test
1084+
New-AzResourceGroup -Name $rgname -Location $rglocation
1085+
1086+
$tagsToCompare = @{
1087+
"MY_FIRST_TAG" = "tagValue";
1088+
"MYFIRSTTAG" = "tagvalue2";
1089+
"mysecondTag" = "tagValue3";
1090+
"mythirdtag" = "tagvalue4"
1091+
}
1092+
1093+
$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile simpleTemplateWithTagsOutput.json -TemplateParameterFile simpleTemplateWithTagsOutputParameters.json
1094+
1095+
# Assert
1096+
Assert-AreEqual Succeeded $deployment.ProvisioningState
1097+
1098+
$subId = (Get-AzContext).Subscription.SubscriptionId
1099+
$deploymentId = "/subscriptions/$subId/resourcegroups/$rgname/providers/Microsoft.Resources/deployments/$rname"
1100+
$getById = Get-AzResourceGroupDeployment -Id $deploymentId
1101+
Assert-AreEqual $getById.DeploymentName $deployment.DeploymentName
1102+
1103+
$tagsOutput = $getById.Outputs.tags.Value
1104+
1105+
$tagsOutputJson = ConvertTo-Json -Compress $tagsOutput
1106+
1107+
# Performs a case sensitive comparison
1108+
# Doing a foreach on the keys and comparing against the JSON string
1109+
# this needed because the flag -AsHashTable doesn't seem to work
1110+
# in ConvertTo-Json cmdlet
1111+
foreach ($tag in $tagsToCompare.Keys)
1112+
{
1113+
Assert-True { $tagsOutputJson -clike "*${tag}*"}
1114+
}
1115+
}
1116+
10071117
finally
10081118
{
10091119
# Cleanup

0 commit comments

Comments
 (0)