diff --git a/source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 b/source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 new file mode 100644 index 00000000..5e1684b9 --- /dev/null +++ b/source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 @@ -0,0 +1,26 @@ +<# + .SYNOPSIS + Helper function that formats Cosmos DB Document QueryParameters' keys to lowercase. +#> +function Format-CosmosDbDocumentQueryParameters +{ + [CmdletBinding()] + [OutputType([Hashtable[]])] + param( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [Hashtable[]] + $QueryParameters + ) + + $output = foreach ($hashtable in $QueryParameters) + { + $item_ht = @{} + $hashtable.GetEnumerator() | ForEach-Object { + $item_ht.add($PSItem.Name.ToLower(), $PSItem.Value) + } + $item_ht + } + + return $output +} diff --git a/source/Public/documents/Get-CosmosDbDocumentJson.ps1 b/source/Public/documents/Get-CosmosDbDocumentJson.ps1 index c117af20..25945b28 100644 --- a/source/Public/documents/Get-CosmosDbDocumentJson.ps1 +++ b/source/Public/documents/Get-CosmosDbDocumentJson.ps1 @@ -143,6 +143,11 @@ function Get-CosmosDbDocumentJson if (-not [System.String]::IsNullOrEmpty($QueryParameters)) { + if ($QueryParameters.keys -cmatch "[A-Z]") + { + Write-Warning ($LocalizedData.WarningDocumentQueryParameterKeysNotLowercase) + $QueryParameters = Format-CosmosDbDocumentQueryParameters $QueryParameters + } $bodyObject += @{ parameters = $QueryParameters } } diff --git a/source/en-US/CosmosDB.strings.psd1 b/source/en-US/CosmosDB.strings.psd1 index cb9ba4a0..e779f851 100644 --- a/source/en-US/CosmosDB.strings.psd1 +++ b/source/en-US/CosmosDB.strings.psd1 @@ -61,4 +61,5 @@ ConvertFrom-StringData -StringData @' ErrorNewDatabaseThroughputParameterConflict = Both 'OfferThroughput' and 'AutoscaleThroughput' should not be specified when creating a new database. DeprecateAttachmentWarning = Attachments are a legacy feature. Their support is scoped to offer continued functionality if you are already using this feature. See https://aka.ms/cosmosdb-attachments for more information. ErrorConvertingDocumentJsonToObject = An error occured converting the document information returned from Cosmsos DB into an object. This might be caused by the document including keys with same name but differing in case. Include the -ReturnJson parameter to return these as JSON instead. + WarningDocumentQueryParameterKeysNotLowercase = CosmosDbDocument QueryParameters keys (name, value) should be entirely lower case '@ diff --git a/tests/Unit/CosmosDB.documents.Tests.ps1 b/tests/Unit/CosmosDB.documents.Tests.ps1 index 4b50c1fd..d0e61fd6 100644 --- a/tests/Unit/CosmosDB.documents.Tests.ps1 +++ b/tests/Unit/CosmosDB.documents.Tests.ps1 @@ -223,6 +223,35 @@ InModuleScope $ProjectName { } } + Describe 'Format-CosmosDbDocumentQueryParameters' -Tag 'Unit' { + It 'Should exist' { + { Get-Command -Name Format-CosmosDbDocumentQueryParameters -ErrorAction Stop } | Should -Not -Throw + } + + Context 'When called with a hashtable' { + It 'Should convert hashtable keys to lower case' { + $Actual = Format-CosmosDbDocumentQueryParameters -QueryParameters @{Name = "One"; Value = "Two" } + ($Actual.Keys | Sort-Object ) -join ',' | Should -BeExactly "name,value" + } + + It 'Should not change hashtable values' { + $Actual = Format-CosmosDbDocumentQueryParameters -QueryParameters @{Name = "1qaz2wsx3edc!QAZ@WSX#EDC"; Value = "/.,?>