SimonWahlin / AzExpression

Invoke Azure ARM functions locally

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty Strings Accepted in ARM template but not in the New-AzUniqueString function

JorgeDios opened this issue · comments

Hello,

First of all, thank you very much for your module which is making my life a lot easier dealing with the unit and integration tests.

I've noticed though a difference between the behaviour in ARM and in your "New-AzUniqueString" function: ARM accepts empty strings as parameters and its result varies whether empty strings are passed or not.

Examples:

  • ARM
uniqueString('test')     --> "rbgf3xv4ufgzg"
uniqueString('test', '')  --> "ztqsfnyb6v7h6"
  • New-AzUniqueString
New-AzUniqueString -InputStrings @('test')  --> "rbgf3xv4ufgzg"
New-AzUniqueString -InputStrings @('test','') --> New-AzUniqueString: Cannot bind argument to parameter 'InputStrings' because it is an empty string.

Suggested Code:

function New-AzUniqueString {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$false)]
        [string[]]$InputStrings
    )
    
    if ($InputStrings) {
        $parameters = [Azure.Deployments.Expression.Expressions.FunctionArgument[]]::new($InputStrings.Count)
        (0..($InputStrings.Count-1)) | ForEach-Object { $parameters[$PSItem] = [Newtonsoft.Json.Linq.JValue]::new($InputStrings[$PSItem]) }
        
        $expressionBuiltInFunction = [Azure.Deployments.Expression.Expressions.ExpressionBuiltInFunctions]::new()
        return ($expressionBuiltInFunction.EvaluateFunction(
            "uniqueString",
            $parameters,
            [Azure.Deployments.Expression.Expressions.ExpressionEvaluationContext]::new()
        ).ToString())
    }
}

Thank you @JorgeDios that is a great find!
I'd prefer to keep the parameter mandatory, but I think just adding the AllowEmptyString() decorator would do the trick.
I'll give it a try and do a new release.

Seems to be working, I just released 0.2.7 to PowerShell gallery.

@SimonWahlin Thank you for the quick fix (0.2.7). AllowEmptyString() is indeed the way to go.