From b9d45d126c2529d2a65ee1dea81c773f2fb1375f Mon Sep 17 00:00:00 2001 From: Ilya Date: Wed, 18 Dec 2019 22:34:16 +0500 Subject: [PATCH 1/4] Migrate Test-Json to System.Text.Json API --- .../commands/utility/TestJsonCommand.cs | 20 ++++--- .../Test-Json.Tests.ps1 | 57 ++++++++++++------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index 668c5072841..ab6641113f1 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -8,9 +8,12 @@ using System.Reflection; using System.Runtime.ExceptionServices; using System.Security; -using Newtonsoft.Json.Linq; +using System.Text.Json; + using NJsonSchema; +#nullable enable + namespace Microsoft.PowerShell.Commands { /// @@ -26,7 +29,7 @@ public class TestJsonCommand : PSCmdlet /// Gets or sets JSON string to be validated. /// [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true)] - public string Json { get; set; } + public string Json { get; set; } = default!; /// /// Gets or sets schema to validate the JSON against. @@ -38,7 +41,7 @@ public class TestJsonCommand : PSCmdlet /// [Parameter(Position = 1, ParameterSetName = SchemaStringParameterSet)] [ValidateNotNullOrEmpty] - public string Schema { get; set; } + public string? Schema { get; set; } /// /// Gets or sets path to the file containg schema to validate the JSON string against. @@ -46,9 +49,9 @@ public class TestJsonCommand : PSCmdlet /// [Parameter(Position = 1, ParameterSetName = SchemaFileParameterSet)] [ValidateNotNullOrEmpty] - public string SchemaFile { get; set; } + public string? SchemaFile { get; set; } - private JsonSchema _jschema; + private JsonSchema? _jschema; /// /// Process all exceptions in the AggregateException. @@ -59,7 +62,7 @@ public class TestJsonCommand : PSCmdlet /// Return value is unreachable since we always rethrow. private static bool UnwrapException(Exception e) { - if (e is TargetInvocationException) + if (e is TargetInvocationException && e.InnerException != null) { ExceptionDispatchInfo.Capture(e.InnerException).Throw(); } @@ -135,16 +138,15 @@ e is SecurityException /// protected override void ProcessRecord() { - JObject parsedJson = null; bool result = true; try { - parsedJson = JObject.Parse(Json); + JsonDocument.Parse(Json); if (_jschema != null) { - var errorMessages = _jschema.Validate(parsedJson); + var errorMessages = _jschema.Validate(Json); if (errorMessages != null && errorMessages.Count != 0) { result = false; 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..c35d611bf46 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -11,13 +11,13 @@ Describe "Test-Json" -Tags "CI" { $validSchemaJson = @" { - 'description': 'A person', - 'type': 'object', - 'properties': { - 'name': {'type': 'string'}, - 'hobbies': { - 'type': 'array', - 'items': {'type': 'string'} + "description": "A person", + "type": "object", + "properties": { + "name": {"type": "string"}, + "hobbies": { + "type": "array", + "items": {"type": "string"} } } } @@ -25,13 +25,13 @@ Describe "Test-Json" -Tags "CI" { $invalidSchemaJson = @" { - 'description', - 'type': 'object', - 'properties': { - 'name': {'type': 'string'}, - 'hobbies': { - 'type': 'array', - 'items': {'type': 'string'} + "description", + "type": "object", + "properties": { + "name": {"type": "string"}, + "hobbies": { + "type": "array", + "items": {"type": "string"} } } } @@ -39,29 +39,29 @@ Describe "Test-Json" -Tags "CI" { $validJson = @" { - 'name': 'James', - 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + "name": "James", + "hobbies": [".NET", "Blogging", "Reading", "Xbox", "LOLCATS"] } "@ $invalidTypeInJson = @" { - 'name': 123, - 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + "name": 123, + "hobbies": [".NET", "Blogging", "Reading", "Xbox", "LOLCATS"] } "@ $invalidTypeInJson2 = @" { - 'name': 123, - 'hobbies': [456, 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + "name": 123, + "hobbies": [456, "Blogging", "Reading", "Xbox", "LOLCATS"] } "@ $invalidNodeInJson = @" { - 'name': 'James', - 'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS'] + "name": "James", + "hobbies": [".NET", "Blogging", "Reading", "Xbox", "LOLCATS"] errorNode } "@ @@ -152,4 +152,17 @@ 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 primitives: " -TestCases @( + @{ name = 'number'; value = 1 } + @{ name = '"true"'; value = '"true"' } + @{ name = '"false"'; value = '"false"' } + @{ name = '"null"'; value = '"null"' } + @{ name = 'string'; value = '"abc"' } + @{ name = 'array'; value = '[ 1, 2 ]' } + ) { + param ($name, $value) + + Test-Json -Json $value | Should -BeTrue + } } From 85f13b34bf1f96bef2750888ef6db52861b3ee35 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 30 Apr 2020 16:42:05 +0500 Subject: [PATCH 2/4] Style fix --- .../commands/utility/TestJsonCommand.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index ab6641113f1..bd2dc37d5c9 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +#nullable enable + using System; using System.Globalization; using System.IO; @@ -12,8 +14,6 @@ using NJsonSchema; -#nullable enable - namespace Microsoft.PowerShell.Commands { /// From 1adb626f8b6bc6f22dad6de3aecb1d325b4c9edb Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 30 Apr 2020 21:30:47 +0500 Subject: [PATCH 3/4] Add new tests --- .../Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 | 3 +++ 1 file changed, 3 insertions(+) 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 c35d611bf46..e0c8a758005 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -156,8 +156,11 @@ Describe "Test-Json" -Tags "CI" { It "Test-Json recognizes primitives: " -TestCases @( @{ name = 'number'; value = 1 } @{ name = '"true"'; value = '"true"' } + @{ name = 'true'; value = 'true' } @{ name = '"false"'; value = '"false"' } + @{ name = 'false'; value = 'false' } @{ name = '"null"'; value = '"null"' } + @{ name = 'null'; value = 'null' } @{ name = 'string'; value = '"abc"' } @{ name = 'array'; value = '[ 1, 2 ]' } ) { From ae405003a93417804e7f1838c06ad6c21f4edf15 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 27 Oct 2020 08:16:51 +0500 Subject: [PATCH 4/4] Use ExceptionDispatchInfo.Throw() --- .../commands/utility/TestJsonCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index bd2dc37d5c9..a19906259a3 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -64,7 +64,7 @@ private static bool UnwrapException(Exception e) { if (e is TargetInvocationException && e.InnerException != null) { - ExceptionDispatchInfo.Capture(e.InnerException).Throw(); + ExceptionDispatchInfo.Throw(e.InnerException); } else {