Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IgnoreComments and AllowTrailingCommas options to Test-Json cmdlet #23817

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,12 +98,22 @@ public string LiteralPath
[ValidateNotNullOrEmpty]
public string SchemaFile { get; set; }

/// <summary>
/// Gets or sets JSON document options.
/// </summary>
/// <value></value>
ArmaanMcleod marked this conversation as resolved.
Show resolved Hide resolved
[Parameter]
[ValidateNotNullOrEmpty]
[ValidateSet("IgnoreComments", "AllowTrailingCommas")]
public string[] Options { get; set; } = Array.Empty<string>();

#endregion

#region Private Members

private bool _isLiteralPath = false;
private JsonSchema _jschema;
private JsonDocumentOptions _documentOptions;

#endregion

Expand Down Expand Up @@ -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 = Options.Contains("IgnoreComments", StringComparer.OrdinalIgnoreCase)
? JsonCommentHandling.Skip
: JsonCommentHandling.Disallow,
AllowTrailingCommas = Options.Contains("AllowTrailingCommas", StringComparer.OrdinalIgnoreCase)
iSazonov marked this conversation as resolved.
Show resolved Hide resolved
};
}

/// <summary>
Expand Down Expand Up @@ -235,7 +254,7 @@ protected override void ProcessRecord()
try
{

var parsedJson = JsonNode.Parse(jsonToParse);
var parsedJson = JsonNode.Parse(jsonToParse, null, _documentOptions);
ArmaanMcleod marked this conversation as resolved.
Show resolved Hide resolved

if (_jschema != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 '<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
}
}