From 625a5bb90ad06688d12f3fd1301bcd2dc21bc8f1 Mon Sep 17 00:00:00 2001 From: DeviRich <1254758-devirich@users.noreply.gitlab.com> Date: Thu, 21 Oct 2021 22:21:23 -0600 Subject: [PATCH 1/3] Still no -cnotmatch test --- ...Format-CosmosDbDocumentQueryParameters.ps1 | 26 +++++++++++++++++ .../documents/Get-CosmosDbDocumentJson.ps1 | 5 ++++ source/en-US/CosmosDB.strings.psd1 | 1 + tests/Unit/CosmosDB.documents.Tests.ps1 | 29 +++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 diff --git a/source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 b/source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 new file mode 100644 index 00000000..e33d4a61 --- /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..9933a39d 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 -cnotmatch "[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 = "/.,?> Date: Thu, 21 Oct 2021 23:06:54 -0600 Subject: [PATCH 2/3] fix: matching is hard --- source/Public/documents/Get-CosmosDbDocumentJson.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/Public/documents/Get-CosmosDbDocumentJson.ps1 b/source/Public/documents/Get-CosmosDbDocumentJson.ps1 index 9933a39d..25945b28 100644 --- a/source/Public/documents/Get-CosmosDbDocumentJson.ps1 +++ b/source/Public/documents/Get-CosmosDbDocumentJson.ps1 @@ -143,7 +143,7 @@ function Get-CosmosDbDocumentJson if (-not [System.String]::IsNullOrEmpty($QueryParameters)) { - if ($QueryParameters.keys -cnotmatch "[a-z]") + if ($QueryParameters.keys -cmatch "[A-Z]") { Write-Warning ($LocalizedData.WarningDocumentQueryParameterKeysNotLowercase) $QueryParameters = Format-CosmosDbDocumentQueryParameters $QueryParameters From dcdceeb07b4396c7f4113a7856b29aa3d12493ca Mon Sep 17 00:00:00 2001 From: DeviRich <1254758-devirich@users.noreply.gitlab.com> Date: Thu, 21 Oct 2021 23:16:48 -0600 Subject: [PATCH 3/3] fix: formatting --- .../documents/Format-CosmosDbDocumentQueryParameters.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 b/source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 index e33d4a61..5e1684b9 100644 --- a/source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 +++ b/source/Private/documents/Format-CosmosDbDocumentQueryParameters.ps1 @@ -13,14 +13,14 @@ function Format-CosmosDbDocumentQueryParameters $QueryParameters ) - $Output = foreach ($hashtable in $QueryParameters) + $output = foreach ($hashtable in $QueryParameters) { $item_ht = @{} - $hashtable.getenumerator() | ForEach-Object { + $hashtable.GetEnumerator() | ForEach-Object { $item_ht.add($PSItem.Name.ToLower(), $PSItem.Value) } $item_ht } - return $Output + return $output }