Treat large Enum values as numbers in ConvertTo-Json (#20999)

This commit is contained in:
Jordan Borean 2024-09-20 03:24:20 +10:00 committed by GitHub
parent 805e621517
commit 3e3d83cfa4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 64 additions and 20 deletions

View File

@ -2,6 +2,7 @@
"PSFeedbackProvider",
"PSLoadAssemblyFromNativeCode",
"PSNativeWindowsTildeExpansion",
"PSSerializeJSONLongEnumAsNumber",
"PSRedirectToVariable",
"PSSubsystemPluginModel"
]

View File

@ -2,6 +2,7 @@
"PSFeedbackProvider",
"PSLoadAssemblyFromNativeCode",
"PSNativeWindowsTildeExpansion",
"PSSerializeJSONLongEnumAsNumber",
"PSRedirectToVariable",
"PSSubsystemPluginModel"
]

View File

@ -577,7 +577,7 @@ namespace Microsoft.PowerShell.Commands
{
Type t = obj.GetType();
if (t.IsPrimitive)
if (t.IsPrimitive || (t.IsEnum && ExperimentalFeature.IsEnabled(ExperimentalFeature.PSSerializeJSONLongEnumAsNumber)))
{
rv = obj;
}

View File

@ -24,6 +24,7 @@ namespace System.Management.Automation
internal const string PSFeedbackProvider = "PSFeedbackProvider";
internal const string PSNativeWindowsTildeExpansion = nameof(PSNativeWindowsTildeExpansion);
internal const string PSRedirectToVariable = "PSRedirectToVariable";
internal const string PSSerializeJSONLongEnumAsNumber = nameof(PSSerializeJSONLongEnumAsNumber);
#endregion
@ -121,6 +122,10 @@ namespace System.Management.Automation
new ExperimentalFeature(
name: PSRedirectToVariable,
description: "Add support for redirecting to the variable drive"),
new ExperimentalFeature(
name: PSSerializeJSONLongEnumAsNumber,
description: "Serialize enums based on long or ulong as an numeric value rather than the string representation when using ConvertTo-Json."
)
};
EngineExperimentalFeatures = new ReadOnlyCollection<ExperimentalFeature>(engineFeatures);

View File

@ -0,0 +1,34 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'ConvertTo-Json with PSSerializeJSONLongEnumAsNumber' -tags "CI" {
BeforeAll {
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues['It:Skip'] = -not [ExperimentalFeature]::IsEnabled('PSSerializeJSONLongEnumAsNumber')
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
It 'Should treat enums as integers' {
enum LongEnum : long {
LongValue = -1
}
enum ULongEnum : ulong {
ULongValue = 18446744073709551615
}
$obj = [Ordered]@{
Long = [LongEnum]::LongValue
ULong = [ULongEnum]::ULongValue
}
$actual = ConvertTo-Json -InputObject $obj -Compress
$actual | Should -Be '{"Long":-1,"ULong":18446744073709551615}'
$actual = ConvertTo-Json -InputObject $obj -EnumsAsStrings -Compress
$actual | Should -Be '{"Long":"LongValue","ULong":"ULongValue"}'
}
}

View File

@ -58,7 +58,9 @@ Describe "Json Tests" -Tags "Feature" {
$valueFromNotCompressedResult.FirstName | Should -Match $valueFromCompressedResult.FirstName
}
It "Convertto-Json should handle Enum based on Int64" {
It "Convertto-Json should handle Enum based on Int64" -Skip:(
[ExperimentalFeature]::IsEnabled("PSSerializeJSONLongEnumAsNumber")
) {
# Test follow-up for bug Win8: 378368 Convertto-Json problems with Enum based on Int64.
if ( $null -eq ("JsonEnumTest" -as "Type")) {

View File

@ -3,6 +3,7 @@
"ExpTest.FeatureOne": [ "test/powershell/engine/ExperimentalFeature/ExperimentalFeature.Basic.Tests.ps1" ],
"PSCultureInvariantReplaceOperator": [ "test/powershell/Language/Operators/ReplaceOperator.Tests.ps1" ],
"Microsoft.PowerShell.Utility.PSManageBreakpointsInRunspace": [ "test/powershell/Modules/Microsoft.PowerShell.Utility/RunspaceBreakpointManagement.Tests.ps1" ],
"PSNativeWindowsTildeExpansion": [ "test/powershell/Language/Scripting/NativeExecution/NativeWindowsTildeExpansion.Tests.ps1" ]
"PSNativeWindowsTildeExpansion": [ "test/powershell/Language/Scripting/NativeExecution/NativeWindowsTildeExpansion.Tests.ps1" ],
"PSSerializeJSONLongEnumAsNumber": [ "test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.PSSerializeJSONLongEnumAsNumber.Tests.ps1" ]
}
}