Skip to content

Commit 0af060f

Browse files
committed
Add swithces for enabling NSJ deserialization
1 parent de3b92c commit 0af060f

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
#if !NET9_0_OR_GREATER
5+
namespace System.Runtime.CompilerServices
6+
{
7+
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
8+
internal sealed class FeatureSwitchDefinitionAttribute : Attribute
9+
{
10+
public FeatureSwitchDefinitionAttribute(string switchName) => SwitchName = switchName;
11+
public string SwitchName { get; }
12+
}
13+
}
14+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace NuGet.Common
8+
{
9+
public static class NuGetFeatureFlags
10+
{
11+
internal const string UsesNSJDeserializationSwitchName = "NuGet.UsesNSJDeserialization";
12+
internal const string UsesNSJDeserializationEnvVar = "NUGET_USES_NSJ_DESERIALIZATION";
13+
14+
/// <summary>
15+
/// <see langword="true"/> to use Newtonsoft.Json (default); <see langword="false"/> to use System.Text.Json.
16+
/// Override via AppContext switch <c>NuGet.UsesNSJDeserialization</c> or env var <c>NUGET_USES_NSJ_DESERIALIZATION</c>.
17+
/// </summary>
18+
[FeatureSwitchDefinition(UsesNSJDeserializationSwitchName)]
19+
public static bool UsesNSJDeserialization { get; } = GetSwitch(EnvironmentVariableWrapper.Instance);
20+
21+
internal static bool GetSwitch(IEnvironmentVariableReader env)
22+
{
23+
if (AppContext.TryGetSwitch(UsesNSJDeserializationSwitchName, out bool switchValue))
24+
{
25+
return switchValue;
26+
}
27+
28+
string? envValue = env.GetEnvironmentVariable(UsesNSJDeserializationEnvVar);
29+
return !string.Equals(envValue, "false", StringComparison.OrdinalIgnoreCase);
30+
}
31+
}
32+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
#nullable enable
2+
NuGet.Common.NuGetFeatureFlags
3+
static NuGet.Common.NuGetFeatureFlags.UsesNSJDeserialization.get -> bool
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using Test.Utility;
6+
using Xunit;
7+
8+
namespace NuGet.Common.Test
9+
{
10+
public class NuGetFeatureFlagsTests
11+
{
12+
[Fact]
13+
public void GetSwitch_WhenEnvVarNotSet_ReturnsTrue()
14+
{
15+
bool result = NuGetFeatureFlags.GetSwitch(TestEnvironmentVariableReader.EmptyInstance);
16+
17+
Assert.True(result);
18+
}
19+
20+
[Theory]
21+
[InlineData("false")]
22+
[InlineData("False")]
23+
[InlineData("FALSE")]
24+
public void GetSwitch_WhenEnvVarSetToFalse_ReturnsFalse(string value)
25+
{
26+
var env = new TestEnvironmentVariableReader(
27+
new Dictionary<string, string> { [NuGetFeatureFlags.UsesNSJDeserializationEnvVar] = value });
28+
29+
bool result = NuGetFeatureFlags.GetSwitch(env);
30+
31+
Assert.False(result);
32+
}
33+
34+
[Theory]
35+
[InlineData("true")]
36+
[InlineData("True")]
37+
[InlineData("0")]
38+
[InlineData("1")]
39+
[InlineData("anything")]
40+
public void GetSwitch_WhenEnvVarSetToTrueOrUnrecognized_ReturnsTrue(string value)
41+
{
42+
var env = new TestEnvironmentVariableReader(
43+
new Dictionary<string, string> { [NuGetFeatureFlags.UsesNSJDeserializationEnvVar] = value });
44+
45+
bool result = NuGetFeatureFlags.GetSwitch(env);
46+
47+
Assert.True(result);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)