Azure / PSRule.Rules.Azure

Rules to validate Azure resources and infrastructure as code (IaC) using PSRule.
https://azure.github.io/PSRule.Rules.Azure/
MIT License
389 stars 84 forks source link

length() is incorrect when applied to object #2263

Closed dmgonch closed 1 year ago

dmgonch commented 1 year ago

Description of the issue

Here is the bicep that reproduces the issue:

param location string = resourceGroup().location

var things = {
  THE_ONLY_THING: 'the_one'
}

var tasks = [
  {
    name: 'process-thing'
    parameters: union(
      length(things) != 1 ? {
        Things1Name: '!!! UNEXPECTED_NAME !!!'
        Things1Value: '!!! UNEXPECTED_VALUE !!!'
      } : {},
      length(things) == 1 ? {
        Things1Name: items(things)[0].key
        Things1Value: items(things)[0].value
      } : {}
    )
  }
]

resource taskDeployment 'Microsoft.Resources/deployments@2020-10-01' = {
  name: 'name'
  location: location
  properties: {
    mode: 'Incremental'
    parameters: {
      tasks: tasks
      thingsCount: length(things)
    }
  }
}

To Reproduce

Steps to reproduce the issue:

bicep build bug.bicep; powershell -noprofile Export-AzRuleTemplateData -TemplateFile bug.json -OutputPath bug1-export.json

Expected behaviour

Expected the following parameters to be generated:

{
            "mode": "Incremental",
            "parameters": {
                "tasks": [
                    {
                        "name": "process-thing",
                        "parameters": {
                            "Things1Name": "THE_ONLY_THING",
                            "Things1Value": "the_one"
                        }
                    }
                ],
                "thingsCount": 1
            }
        }

Error output Instead the following JSON is generated:

{
            "mode": "Incremental",
            "parameters": {
                "tasks": [
                    {
                        "name": "process-thing",
                        "parameters": {
                            "Things1Name": "!!! UNEXPECTED_NAME !!!",
                            "Things1Value": "!!! UNEXPECTED_VALUE !!!"
                        }
                    }
                ],
                "thingsCount": 12
            }
        }

Module in use and version:

Captured output from $PSVersionTable:

Name                           Value
----                           -----
PSVersion                      5.1.22621.963
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.963
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Additional context The workaround appears to be to use

length(items(things))
BernieWhite commented 1 year ago

@dmgonch Thanks for the repro.