From 519ec516060b45a9a71190bac9b30fff4c220e5a Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski Date: Thu, 21 Jul 2022 23:48:33 +0200 Subject: [PATCH 1/2] Fix Test-Json not handling non-object types at root --- .../commands/utility/TestJsonCommand.cs | 3 +-- .../Test-Json.Tests.ps1 | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index 488cb4ab4ab..9d792b91233 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -136,12 +136,11 @@ e is SecurityException /// protected override void ProcessRecord() { - JObject parsedJson = null; bool result = true; try { - parsedJson = JObject.Parse(Json); + var parsedJson = JToken.Parse(Json); if (_jschema != null) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 index d7653d7c1f3..7326be0211e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -65,7 +65,7 @@ Describe "Test-Json" -Tags "CI" { errorNode } "@ -} + } It "Missing JSON schema file doesn't exist" { Test-Path -LiteralPath $missingSchemaJsonPath | Should -BeFalse @@ -152,4 +152,28 @@ Describe "Test-Json" -Tags "CI" { $errorVar[0].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand" $errorVar[1].FullyQualifiedErrorId | Should -BeExactly "InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand" } + + It 'Test-Json recognizes non-object types: ' -TestCases @( + @{ name = 'number'; value = 1; expected = 'number' } + @{ name = '"true"'; value = '"true"'; expected = 'string' } + @{ name = 'true'; value = 'true'; expected = 'boolean' } + @{ name = '"false"'; value = '"false"'; expected = 'string' } + @{ name = 'false'; value = 'false'; expected = 'boolean' } + @{ name = '"null"'; value = '"null"'; expected = 'string' } + @{ name = 'null'; value = 'null'; expected = 'null' } + @{ name = 'string'; value = '"abc"'; expected = 'string' } + @{ name = 'array'; value = '[ 1, 2 ]'; expected = 'array' } + ) { + param ($name, $value, $expected) + + # All JSON valid + Test-Json -Json $value | Should -BeTrue + + # Exactly one type should match + $types = 'string', 'number', 'boolean', 'null', 'array', 'object' + $types | ?{ + $schema = "{ 'type': '$_' }" + Test-Json -Json $value -Schema $schema -ErrorAction SilentlyContinue + } | Should -Be $expected + } } From 674594ef900d72d3da70e0caa60e9fae20c02298 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski Date: Fri, 22 Jul 2022 08:43:31 +0200 Subject: [PATCH 2/2] Replace question mark alias with `Where-Object` Co-authored-by: Ilya --- .../Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 index 7326be0211e..2ea76f5826e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -171,7 +171,7 @@ Describe "Test-Json" -Tags "CI" { # Exactly one type should match $types = 'string', 'number', 'boolean', 'null', 'array', 'object' - $types | ?{ + $types | Where-Object { $schema = "{ 'type': '$_' }" Test-Json -Json $value -Schema $schema -ErrorAction SilentlyContinue } | Should -Be $expected