Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept document QueryParameters with uppercase letters #447

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions source/Public/documents/Get-CosmosDbDocumentJson.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
1 change: 1 addition & 0 deletions source/en-US/CosmosDB.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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
'@
29 changes: 29 additions & 0 deletions tests/Unit/CosmosDB.documents.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/.,?><l;'L:op[OP{" }
($Actual.Values | Sort-Object ) -join ',' | Should -BeExactly "/.,?><l;'L:op[OP{,1qaz2wsx3edc!QAZ@WSX#EDC"
}
}
Context 'When called with a hashtable[]' {
It 'Should convert hashtable keys to lower case' {
$Actual = Format-CosmosDbDocumentQueryParameters -QueryParameters @(@{Name = "One"; Value = "Two" }, @{Name = "Three"; Value = "Four" })
($Actual.Keys | Sort-Object ) -join ',' | Should -BeExactly "name,name,value,value"
}

It 'Should not change hashtable values' {
$Actual = Format-CosmosDbDocumentQueryParameters -QueryParameters @(@{Name = "1qaz2wsx3edc!QAZ@WSX#EDC"; Value = "/.,?><l;'L:op[OP{" }, @{Name = "Two"; Value = "Three" })
($Actual.Values | Sort-Object ) -join ',' | Should -BeExactly "/.,?><l;'L:op[OP{,1qaz2wsx3edc!QAZ@WSX#EDC,Three,Two"
}
}
}

Describe 'Get-CosmosDbDocumentResourcePath' -Tag 'Unit' {
It 'Should exist' {
{ Get-Command -Name Get-CosmosDbDocumentResourcePath -ErrorAction Stop } | Should -Not -Throw
Expand Down