Azure / azure-functions-powershell-worker

PowerShell language worker for Azure Functions.
MIT License
206 stars 54 forks source link

Incorrect workaround in "Upgrading your Azure Function Apps to run on PowerShell 7.4" #1090

Closed azureskydiver closed 1 month ago

azureskydiver commented 1 month ago

In the documentation for Upgrading your Azure Function Apps to run on PowerShell 7.4, it says:

If you cannot guarantee the case of the property names in the JSON payload because your client can use different casing and you are not in control of the client, you can convert the OrderedHashtable to a case-insensitive Hashtable using the following code:

$Request.Body = [HashTable]::New($Request.Body, [StringComparer]::OrdinalIgnoreCase)

Unfortunately, this doesn't work.

To reproduce the problem:

  1. Go to portal.azure.com and create a new HTTP triggered PowerShell based Azure Power Function. (Ensure it is using 7.4)
  2. Use the default generated code, and then add the following after line 7:

    $Request.Body = [HashTable]::New($Request.Body, [StringComparer]::OrdinalIgnoreCase)

Code will look something like this:

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

$Request.Body = [HashTable]::New($Request.Body, [StringComparer]::OrdinalIgnoreCase)

# Interact with query parameters or the body of the request.
$name = $Request.Query.Name
if (-not $name) {
    $name = $Request.Body.Name
}

$body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."

if ($name) {
    $body = "Hello, $name. This HTTP triggered function executed successfully."
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})
  1. Save the code.
  2. Press Test/Run and take the default inputs where JSON payload is:
    {
    "name": "Azure"
    }

Expected results: Output to say "Hello, Azure. This HTTP triggered function executed successfully."

Actual results: Output says: "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."

andystaples commented 1 month ago

Thank you for the feedback. The reason for this behavior is that $request.Body is readonly, and can't be updated with assignment. Please replace with this: $convertedBody = [HashTable]::New($Request.Body, [StringComparer]::OrdinalIgnoreCase) and use $convertedBody instead of $request.Body in the rest of your code. We have updated the wiki to be correct

andystaples commented 1 month ago

Closing with resolution