Emit warning if ConvertTo-Json
exceeds -Depth
value (#13692)
This commit is contained in:
parent
36ba1092a8
commit
9bf512ff5d
|
@ -457,6 +457,7 @@ namespace Microsoft.PowerShell.Commands
|
|||
{
|
||||
// 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
|
||||
{
|
||||
|
@ -484,6 +485,8 @@ namespace Microsoft.PowerShell.Commands
|
|||
}
|
||||
}
|
||||
|
||||
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.
|
||||
|
@ -561,6 +564,16 @@ namespace Microsoft.PowerShell.Commands
|
|||
{
|
||||
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,
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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"
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue