Azure / Azure/arm-template-parser

Child Resources Parse Bug

Open
#55 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4
Forks
12
PR merge metrics
No merged PRs in 30d

Description

Heyo 👋

I noticed a bug when trying to parse templates introducing resources with child resources.

Example template:
```json
"resources": [
{
"type": "Microsoft.Sql/servers",
"name": "[variables('sqlserverName')]",
...
},
"resources": [
{
"type": "databases",
"name": "[variables('databaseName')]",
...
},
{
"type": "Microsoft.Sql/servers/administrators",
"name": "[concat(variables('sqlserverName'), '/', 'activeDirectory')]",
# ...
},
]
```

The code below contains the bug. Trying to access the wrong key for the parent flattened resource:
```csharp
private Dictionary flattenedResources = new Dictionary(StringComparer.OrdinalIgnoreCase);
...
Later in SaveFlattenedResources
...
if (parentName != null && parentType != null)
{
resource.Path = $"{flattenedResources[$"{parentName} {parentType}"].resource.Path}.resources[{i}]"; // <-- Fails when querying the Dict because the key is wrong.

dictionaryKey = $"{parentName}/{resource.Name.Value} {parentType}/{resource.Type.Value}";
}
else
{
if (resource.Path == "")
{
resource.Path = $"resources[{i}]";
}

dictionaryKey = $"{resource.Name.Value} {resource.Type.Value} {resource.LineNumber}"; // <-- Faulty line. Appending `resource.LineNumber` to the key
}

var resourceExpandedPath = $"{(parentExpandedPath != "" ? parentExpandedPath + "." : "")}resources[{i}]";
flattenedResources.Add(dictionaryKey, (resource, resourceExpandedPath));

if (resource.Resources != null)
{
string resourceNamePrefix = parentName == null ? "" : $"{parentName}/";
string resourceTypePrefix = parentType == null ? "" : $"{parentType}/";

SaveFlattenedResources(resource.Resources, $"{resourceNamePrefix}{resource.Name.Value}", $"{resourceTypePrefix}{resource.Type.Value}", resourceExpandedPath);
}
```

[Easy fix, just change the line](https://github.com/Azure/arm-template-parser/blob/17414c6e4998314b70a698024800d4a43daf71e1/Template.Parser.Core/ArmTemplateProcessor.cs#L296)
`dictionaryKey = $"{resource.Name.Value} {resource.Type.Value} {resource.LineNumber}";`
|
\\/
`dictionaryKey = $"{resource.Name.Value} {resource.Type.Value}";`

Unless you guys know that this is there for a reason and then I don't know how to add the correct `resource.LineNumber` to the key when accessing the dictionary.

Thanks in advance!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.