From fe0836dc798c519fd54eaa0a8de2d13fae556102 Mon Sep 17 00:00:00 2001 From: Adam Gauthier Date: Wed, 28 Nov 2018 04:15:20 -0500 Subject: [PATCH 1/2] Remove workaround for fixed invalid json array deserializing bug --- .../commands/utility/WebCmdlet/JsonObject.cs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs index b7c04b1e58b..a5440ce29b7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs @@ -59,22 +59,6 @@ public static object ConvertFromJson(string input, bool returnHashtable, out Err error = null; try { - // JsonConvert.DeserializeObject does not throw an exception when an invalid Json array is passed. - // This issue is being tracked by https://github.com/JamesNK/Newtonsoft.Json/issues/1321. - // To work around this, we need to identify when input is a Json array, and then try to parse it via JArray.Parse(). - - // If input starts with '[' (ignoring white spaces). - if (Regex.Match(input, @"^\s*\[").Success) - { - // JArray.Parse() will throw a JsonException if the array is invalid. - // This will be caught by the catch block below, and then throw an - // ArgumentException - this is done to have same behavior as the JavaScriptSerializer. - JArray.Parse(input); - - // Please note that if the Json array is valid, we don't do anything, - // we just continue the deserialization. - } - var obj = JsonConvert.DeserializeObject( input, new JsonSerializerSettings From ac76a367c464cd22c981b79618807e122edbb68d Mon Sep 17 00:00:00 2001 From: Adam Gauthier Date: Wed, 28 Nov 2018 21:14:37 -0500 Subject: [PATCH 2/2] [fixup!] Add ConvertFrom-Json test to cover incomplete array exception --- .../Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 index e4a32a5ee88..39aa655b71e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Json.Tests.ps1 @@ -52,4 +52,10 @@ Describe 'ConvertFrom-Json' -tags "CI" { $json | Should -BeOfType Hashtable } } + + It 'Throws an ArgumentException with an incomplete array with AsHashtable switch set to ' -TestCase $testCasesWithAndWithoutAsHashtableSwitch { + Param($AsHashtable) + { ConvertFrom-Json '["1",' -AsHashtable:$AsHashtable } | + Should -Throw -ErrorId "System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand" + } }