Skip to content

[Az Resources] Fix resource tags casing serialization - tags casing not being preserved when passed as parameters #15920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,12 @@ protected override JsonDictionaryContract CreateDictionaryContract(Type objectTy
{
var contract = base.CreateDictionaryContract(objectType);

var attributes = objectType.GetCustomAttributes(attributeType: typeof(JsonPreserveCaseDictionaryAttribute), inherit: true);
if (attributes.Any())
{
// TODO: Remove IfDef code
// TODO: Remove IfDef code
#if NETSTANDARD
contract.DictionaryKeyResolver = keyName => keyName;
contract.DictionaryKeyResolver = keyName => keyName;
#else
contract.PropertyNameResolver = propertyName => propertyName;
contract.PropertyNameResolver = propertyName => propertyName;
#endif
}

return contract;
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/Resources/Resources.Test/Json/PSJsonSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,10 @@ public void Serialize_Hashtable_Success()

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

// NOTE(jcotillo): JsonExtensions is now camelCasing all property keys
// therefore even though Bar was set as PascalCase, after serializing it
// the key became camelCase.
JToken expected = JToken.FromObject(new
{
foo = "fooValue",
bar = true,
Bar = true,
nested = new
{
foo = "4d44fe86-f04a-4ba5-9900-abdec8cb11c1",
Expand Down
14 changes: 14 additions & 0 deletions src/Resources/Resources.Test/ScenarioTests/DeploymentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,19 @@ public void TestNewDeploymentFromTemplateAndParameterFileContainingDatetimeOutpu
{
TestRunner.RunTestScript("Test-NewDeploymentFromTemplateAndParameterFileContainingDatetimeOutput");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestNewDeploymentFromTemplateFileContainingTagsOutput()
{
TestRunner.RunTestScript("Test-NewDeploymentFromTemplateFileContainingTagsOutput");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestNewDeploymentFromTemplateAndParameterFileContainingTagsOutput()
{
TestRunner.RunTestScript("Test-NewDeploymentFromTemplateAndParameterFileContainingTagsOutput");
}
}
}
110 changes: 110 additions & 0 deletions src/Resources/Resources.Test/ScenarioTests/DeploymentTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,116 @@ function Test-NewDeploymentFromTemplateAndParameterFileContainingDatetimeOutput
Assert-AreEqual $datetimeFormatted $datetimeOutput
}

finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}

<#
.SYNOPSIS
Tests deployment via template and parameter file containing a tags with different casing.
#>
function Test-NewDeploymentFromTemplateFileContainingTagsOutput
{
# Setup
$rgname = Get-ResourceGroupName
$rname = Get-ResourceName
$rglocation = "West US 2"

try
{
# Test
New-AzResourceGroup -Name $rgname -Location $rglocation

$tagsToCompare = @{
"MY_FIRST_TAG" = "tagValue";
"MYFIRSTTAG" = "tagvalue2";
"mysecondTag" = "tagValue3";
"mythirdtag" = "tagvalue4"
}

$parameters = @{ "tags"= $tagsToCompare }

$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile simpleTemplateWithTagsOutput.json -TemplateParameterObject $parameters

# Assert
Assert-AreEqual Succeeded $deployment.ProvisioningState

$subId = (Get-AzContext).Subscription.SubscriptionId
$deploymentId = "/subscriptions/$subId/resourcegroups/$rgname/providers/Microsoft.Resources/deployments/$rname"
$getById = Get-AzResourceGroupDeployment -Id $deploymentId
Assert-AreEqual $getById.DeploymentName $deployment.DeploymentName

$tagsOutput = $getById.Outputs.tags.Value

$tagsOutputJson = ConvertTo-Json -Compress $tagsOutput

# Performs a case sensitive comparison
# Doing a foreach on the keys and comparing against the JSON string
# this needed because the flag -AsHashTable doesn't seem to work
# in ConvertTo-Json cmdlet
foreach ($tag in $tagsToCompare.Keys)
{
Assert-True { $tagsOutputJson -clike "*${tag}*"}
}
}

finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}

<#
.SYNOPSIS
Tests deployment via template and parameter file containing a tags with different casing.
#>
function Test-NewDeploymentFromTemplateAndParameterFileContainingTagsOutput
{
# Setup
$rgname = Get-ResourceGroupName
$rname = Get-ResourceName
$rglocation = "West US 2"

try
{
# Test
New-AzResourceGroup -Name $rgname -Location $rglocation

$tagsToCompare = @{
"MY_FIRST_TAG" = "tagValue";
"MYFIRSTTAG" = "tagvalue2";
"mysecondTag" = "tagValue3";
"mythirdtag" = "tagvalue4"
}

$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile simpleTemplateWithTagsOutput.json -TemplateParameterFile simpleTemplateWithTagsOutputParameters.json

# Assert
Assert-AreEqual Succeeded $deployment.ProvisioningState

$subId = (Get-AzContext).Subscription.SubscriptionId
$deploymentId = "/subscriptions/$subId/resourcegroups/$rgname/providers/Microsoft.Resources/deployments/$rname"
$getById = Get-AzResourceGroupDeployment -Id $deploymentId
Assert-AreEqual $getById.DeploymentName $deployment.DeploymentName

$tagsOutput = $getById.Outputs.tags.Value

$tagsOutputJson = ConvertTo-Json -Compress $tagsOutput

# Performs a case sensitive comparison
# Doing a foreach on the keys and comparing against the JSON string
# this needed because the flag -AsHashTable doesn't seem to work
# in ConvertTo-Json cmdlet
foreach ($tag in $tagsToCompare.Keys)
{
Assert-True { $tagsOutputJson -clike "*${tag}*"}
}
}

finally
{
# Cleanup
Expand Down
Loading