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 @@ -27,15 +27,13 @@ public class ConvertToJsonCommand : PSCmdlet, IDisposable

private int _depth = 2;

private const int maxDepthAllowed = 100;

private readonly CancellationTokenSource _cancellationSource = new();

/// <summary>
/// Gets or sets the Depth property.
/// </summary>
[Parameter]
[ValidateRange(0, int.MaxValue)]
[ValidateRange(0, 100)]
public int Depth
{
get { return _depth; }
Expand Down Expand Up @@ -99,23 +97,7 @@ protected virtual void Dispose(bool disposing)
_cancellationSource.Dispose();
}
}

/// <summary>
/// Prerequisite checks.
/// </summary>
protected override void BeginProcessing()
{
if (_depth > maxDepthAllowed)
Comment thread
KevRitchie marked this conversation as resolved.
{
string errorMessage = StringUtil.Format(WebCmdletStrings.ReachedMaximumDepthAllowed, maxDepthAllowed);
Comment thread
KevRitchie marked this conversation as resolved.
ThrowTerminatingError(new ErrorRecord(
new InvalidOperationException(errorMessage),
"ReachedMaximumDepthAllowed",
ErrorCategory.InvalidOperation,
null));
}
}


private readonly List<object> _inputObjects = new();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,6 @@
<data name="JsonNetModuleFilesRequired" xml:space="preserve">
<value>Ensure 'Json.Net.psd1' and 'Newtonsoft.Json.dll' are available in a versioned subdirectory of '{0}'.</value>
</data>
<data name="ReachedMaximumDepthAllowed" xml:space="preserve">
<value>The maximum depth allowed for serialization is {0}.</value>
</data>
<data name="JsonDeserializationFailed" xml:space="preserve">
<value>Conversion from JSON failed with error: {0}</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1670,10 +1670,10 @@ Describe "Json Bug fixes" -Tags "Feature" {

$testCases = @(
@{
Name = "ConvertTo-Json -Depth 101 throws MaximumAllowedDepthReached when the user specifies a depth greater than 100."
Name = "ConvertTo-Json -Depth 101 throws ParameterArgumentValidationError when the user specifies a depth greater than 100."
NumberOfElements = 10
MaxDepth = 101
FullyQualifiedErrorId = "ReachedMaximumDepthAllowed,Microsoft.PowerShell.Commands.ConvertToJsonCommand"
FullyQualifiedErrorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertToJsonCommand"
ShouldThrow = $true
}
@{
Expand Down