From 0e4cc863d4bd31dc5f174cbeee2bf9238d8c617f Mon Sep 17 00:00:00 2001 From: ArmaanMcleod Date: Sun, 19 May 2024 22:03:46 +1000 Subject: [PATCH 1/4] Add IgnoreComments and AllowTrailingCommas options to Test-Json --- .../commands/utility/TestJsonCommand.cs | 21 +++++++++++- .../Test-Json.Tests.ps1 | 34 +++++++++++++++++++ 2 files changed, 54 insertions(+), 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 9df1a0055144..53da401b2b51 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -4,6 +4,7 @@ using System; using System.Globalization; using System.IO; +using System.Linq; using System.Management.Automation; using System.Net.Http; using System.Security; @@ -97,12 +98,22 @@ public string LiteralPath [ValidateNotNullOrEmpty] public string SchemaFile { get; set; } + /// + /// Gets or sets JSON document options. + /// + /// + [Parameter] + [ValidateNotNullOrEmpty] + [ValidateSet("IgnoreComments", "AllowTrailingCommas")] + public string[] Option { get; set; } = Array.Empty(); + #endregion #region Private Members private bool _isLiteralPath = false; private JsonSchema _jschema; + private JsonDocumentOptions _documentOptions; #endregion @@ -200,6 +211,14 @@ protected override void BeginProcessing() Exception exception = new(TestJsonCmdletStrings.InvalidJsonSchema, e); ThrowTerminatingError(new ErrorRecord(exception, "InvalidJsonSchema", ErrorCategory.InvalidData, resolvedpath)); } + + _documentOptions = new JsonDocumentOptions + { + CommentHandling = Option.Contains("IgnoreComments", StringComparer.OrdinalIgnoreCase) + ? JsonCommentHandling.Skip + : JsonCommentHandling.Disallow, + AllowTrailingCommas = Option.Contains("AllowTrailingCommas", StringComparer.OrdinalIgnoreCase) + }; } /// @@ -235,7 +254,7 @@ protected override void ProcessRecord() try { - var parsedJson = JsonNode.Parse(jsonToParse); + var parsedJson = JsonNode.Parse(jsonToParse, null, _documentOptions); 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 a5aa52e0296a..32a6cf3ce631 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Test-Json.Tests.ps1 @@ -66,6 +66,26 @@ Describe "Test-Json" -Tags "CI" { } '@ + $jsonWithComments = @' + { + // A Json comment + "string": "test" + } +'@ + + $jsonWithTrailingComma = @' + { + "string": "test", + } +'@ + + $jsonWithCommentsAndTrailingComma = @' + { + // A Json comment + "string": "test", + } +'@ + $validJsonPath = Join-Path -Path $TestDrive -ChildPath 'validJson.json' $validLiteralJsonPath = Join-Path -Path $TestDrive -ChildPath "[valid]Json.json" $invalidNodeInJsonPath = Join-Path -Path $TestDrive -ChildPath 'invalidNodeInJson.json' @@ -309,4 +329,18 @@ Describe "Test-Json" -Tags "CI" { Test-Json -Json $value -Schema $schema -ErrorAction SilentlyContinue } | Should -Be $expected } + + It "Test-Json returns True with document options ''" -TestCases @( + @{ Json = $jsonWithComments; Options = 'IgnoreComments' } + @{ Json = $jsonWithTrailingComma; Options = 'AllowTrailingCommas'} + @{ Json = $jsonWithCommentsAndTrailingComma; Options = 'IgnoreComments', 'AllowTrailingCommas'} + ) { + param($Json, $Options) + + # Without options should fail + ($Json | Test-Json -ErrorAction SilentlyContinue) | Should -BeFalse + + # With options should pass + ($Json | Test-Json -Option $Options -ErrorAction SilentlyContinue) | Should -BeTrue + } } From 014a36ee10c50202a3611b627ea79731f098f758 Mon Sep 17 00:00:00 2001 From: ArmaanMcleod Date: Sat, 15 Jun 2024 19:11:22 +1000 Subject: [PATCH 2/4] Change options parameter to plural --- .../commands/utility/TestJsonCommand.cs | 6 +++--- 1 file changed, 3 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 53da401b2b51..fce750e9a136 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -105,7 +105,7 @@ public string LiteralPath [Parameter] [ValidateNotNullOrEmpty] [ValidateSet("IgnoreComments", "AllowTrailingCommas")] - public string[] Option { get; set; } = Array.Empty(); + public string[] Options { get; set; } = Array.Empty(); #endregion @@ -214,10 +214,10 @@ protected override void BeginProcessing() _documentOptions = new JsonDocumentOptions { - CommentHandling = Option.Contains("IgnoreComments", StringComparer.OrdinalIgnoreCase) + CommentHandling = Options.Contains("IgnoreComments", StringComparer.OrdinalIgnoreCase) ? JsonCommentHandling.Skip : JsonCommentHandling.Disallow, - AllowTrailingCommas = Option.Contains("AllowTrailingCommas", StringComparer.OrdinalIgnoreCase) + AllowTrailingCommas = Options.Contains("AllowTrailingCommas", StringComparer.OrdinalIgnoreCase) }; } From cbbc834e85a194e0716f384372990ef018be01d4 Mon Sep 17 00:00:00 2001 From: ArmaanMcleod Date: Sun, 16 Jun 2024 09:40:54 +1000 Subject: [PATCH 3/4] Fixes from feedback --- .../commands/utility/TestJsonCommand.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs index fce750e9a136..9d188037e803 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -35,6 +35,13 @@ public class TestJsonCommand : PSCmdlet #endregion + #region Json Document Option Constants + + private const string IgnoreCommentsOption = "IgnoreComments"; + private const string AllowTrailingCommasOption = "AllowTrailingCommas"; + + #endregion + #region Parameters /// @@ -101,7 +108,6 @@ public string LiteralPath /// /// Gets or sets JSON document options. /// - /// [Parameter] [ValidateNotNullOrEmpty] [ValidateSet("IgnoreComments", "AllowTrailingCommas")] @@ -214,10 +220,10 @@ protected override void BeginProcessing() _documentOptions = new JsonDocumentOptions { - CommentHandling = Options.Contains("IgnoreComments", StringComparer.OrdinalIgnoreCase) + CommentHandling = Options.Contains(IgnoreCommentsOption, StringComparer.OrdinalIgnoreCase) ? JsonCommentHandling.Skip : JsonCommentHandling.Disallow, - AllowTrailingCommas = Options.Contains("AllowTrailingCommas", StringComparer.OrdinalIgnoreCase) + AllowTrailingCommas = Options.Contains(AllowTrailingCommasOption, StringComparer.OrdinalIgnoreCase) }; } @@ -254,7 +260,7 @@ protected override void ProcessRecord() try { - var parsedJson = JsonNode.Parse(jsonToParse, null, _documentOptions); + var parsedJson = JsonNode.Parse(jsonToParse, nodeOptions: null, _documentOptions); if (_jschema != null) { From 2d424286d025ffeae2d93f3f4994d8f7078a0df0 Mon Sep 17 00:00:00 2001 From: ArmaanMcleod Date: Sun, 16 Jun 2024 09:41:38 +1000 Subject: [PATCH 4/4] Add constants to validate set as well --- .../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 9d188037e803..32603f160f8a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/TestJsonCommand.cs @@ -110,7 +110,7 @@ public string LiteralPath /// [Parameter] [ValidateNotNullOrEmpty] - [ValidateSet("IgnoreComments", "AllowTrailingCommas")] + [ValidateSet(IgnoreCommentsOption, AllowTrailingCommasOption)] public string[] Options { get; set; } = Array.Empty(); #endregion