Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ public static string ConvertToJson(object objectToProcess, in ConvertToJsonConte
{
// Pre-process the object so that it serializes the same, except that properties whose
// values cannot be evaluated are treated as having the value null.
_maxDepthWarningWritten = false;
object preprocessedObject = ProcessValue(objectToProcess, currentDepth: 0, in context);
var jsonSettings = new JsonSerializerSettings
{
Expand Down Expand Up @@ -484,6 +485,8 @@ public static string ConvertToJson(object objectToProcess, in ConvertToJsonConte
}
}

private static bool _maxDepthWarningWritten;

/// <summary>
/// Return an alternate representation of the specified object that serializes the same JSON, except
/// that properties that cannot be evaluated are treated as having the value null.
Expand Down Expand Up @@ -561,6 +564,16 @@ private static object ProcessValue(object obj, int currentDepth, in ConvertToJso
{
if (currentDepth > context.MaxDepth)
{
if (!_maxDepthWarningWritten && context.Cmdlet != null)
{
_maxDepthWarningWritten = true;
string maxDepthMessage = string.Format(
CultureInfo.CurrentCulture,
WebCmdletStrings.JsonMaxDepthReached,
context.MaxDepth);
context.Cmdlet.WriteWarning(maxDepthMessage);
}

if (pso != null && pso.ImmediateBaseObjectIsEmpty)
{
// The obj is a pure PSObject, we convert the original PSObject to a string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,7 @@
<data name="RetryVerboseMsg" xml:space="preserve">
<value>Retrying after interval of {0} seconds. Status code for previous attempt: {1}</value>
</data>
<data name="JsonMaxDepthReached" xml:space="preserve">
<value>Resulting JSON is truncated as serialization has exceeded the set depth of {0}.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
param()

Describe "SecureString conversion tests" -Tags "CI" {
BeforeAll {
$string = "ABCD"
Expand All @@ -24,9 +28,8 @@ Describe "SecureString conversion tests" -Tags "CI" {
}

It "can convert back from a secure string" {
$secret = "abcd"
$ss1 = ConvertTo-SecureString -AsPlainText -Force $secret
$ss1 = ConvertTo-SecureString -AsPlainText -Force $string
$ss2 = ConvertFrom-SecureString $ss1 | ConvertTo-SecureString
$ss2 | ConvertFrom-SecureString -AsPlainText | Should -Be $secret
$ss2 | ConvertFrom-SecureString -AsPlainText | Should -Be $string
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1463,4 +1463,11 @@ Describe "Json Bug fixes" -Tags "Feature" {
$result = "[1,","2,","3]" | ConvertFrom-Json
$result.Count | Should -Be 3
}

It 'ConvertTo-Json will output warning if depth is exceeded.' {
$a = @{ a = @{ b = @{ c = @{ d = 1 } } } }
$json = $a | ConvertTo-Json -Depth 2 -WarningVariable warningMessage -WarningAction SilentlyContinue
$json | Should -Not -BeNullOrEmpty
$warningMessage | Should -Not -BeNullOrEmpty
}
}