CosmosDB icon indicating copy to clipboard operation
CosmosDB copied to clipboard

Broken stored procedure body when using modern JavaScript syntax

Open bak-t opened this issue 7 years ago • 3 comments

Description

When using Set-CosmosDbStoredProcedure/New-CosmosDbStoredProcedure cmdlet to change/create stored procedure the body becomes broken.

For example code:

...
if (someCondition)
  throw new Error(`Root entity (id: ${entity.id}) already exists`);
...

becomes

...
if (someCondition)
  throw new Error(
oot entity (id: ${entity.id}) already exists`);
...

Environment Details

  • Powershell
    • Version: 5.1.15063.1155
    • Host: Console
  • OS: Windows 10 Professional (10.0.15063)
  • CosmosDB module version 2.1.2.514

Additional observations

In Set-CosmosDbStoredProcedure stored procedure body is encoded using:

$StoredProcedureBody = ((($StoredProcedureBody -replace '`n', '\n') -replace '`r', '\r') -replace '"', '\"')

The issue is in search string of -replace operation which should be enclosed in double quotes. Also code to encode body can be replaced with

$StoredProcedureBody = ConvertTo-Json -InputObject $StoredProcedureBody

And even whole REST request body can be built like this:

$requestBody = ConvertTo-Json -Depth 100 -InputObject @{
  id = $id
  body = $StoredProcedureBody
}

P.S.

I hope to prepare pull request during next few days.

bak-t avatar Jul 04 '18 17:07 bak-t

Awesome! Thanks for logging this @bak-t - look forward to the PR! :grin:

PlagueHO avatar Jul 04 '18 19:07 PlagueHO

Due to this difference between Windows PowerShell and PowerShell Core 6.0.x, this issue needs to be reopened. If this difference is addressed in PowerShell Core 6.1 then this issue can be closed again. Otherwise some additional steps will need to be taken to improve the conversion to work on PowerShell Core.

PlagueHO avatar Sep 03 '18 04:09 PlagueHO

Now that PowerShell Core 6.1 is long out we should be able to document and close this issue by defining a minimum recommended PS version and ensuring the tests work correctly with it.

PlagueHO avatar Jun 29 '19 21:06 PlagueHO