diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index 668c5072841..a19906259a3 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; @@ -8,7 +10,8 @@ using System.Reflection; using System.Runtime.ExceptionServices; using System.Security; -using Newtonsoft.Json.Linq; +using System.Text.Json; + using NJsonSchema; 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,9 +62,9 @@ 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(); + ExceptionDispatchInfo.Throw(e.InnerException); } else { @@ -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..e0c8a758005 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,20 @@ 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 = '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 ]' } + ) { + param ($name, $value) + + Test-Json -Json $value | Should -BeTrue + } }