Skip to content

ConfigurationFeatureDefinitionProvider ignores False when there are exists RequirementType #521

@DenomikoN

Description

@DenomikoN

Faced with the issue when due to the overriding configuration ConfigurationFeatureDefinitionProvider will falsely enable a feature flag.
Example setup:
appsettings.json

{
  "FeatureManagement":{
    "FeatureA": {
       "RequirementType": "All",
       "EnabledFor": [{
          "Name":"AlwaysOn"
       }]
    }
  }
}

Overriden by production configuration:
appsettings.production.json

{
  "FeatureManagement:FeatureA": "False" // Disable feature
}

That produces the following configuration key-values pairs:

["FeatureManagement:FeatureA"] = "False",
["FeatureManagement:FeatureA:RequirementType"] = "All",
["FeatureManagement:FeatureA:EnabledFor:0:Name"] = "AlwaysOn"

Expected result: feature manager returns False for IsEnabledAsync("FeatureA")
Test example:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;

namespace TestProject
{
    public class Tests
    {
        [Test]
        public async Task FeatureWithDirectValueMustBeDisabled()
        {
            var configKeys = new Dictionary<string, string?>
            {
                ["FeatureManagement:FeatureA"] = "False",
                ["FeatureManagement:FeatureA:RequirementType"] = "All",
                ["FeatureManagement:FeatureA:EnabledFor:0:Name"] = "AlwaysOn"
            };
            var cfg = new ConfigurationBuilder()
                            .AddInMemoryCollection(configKeys)
                            .Build();

            var sc = new ServiceCollection();
            sc.AddFeatureManagement();
            sc.AddSingleton<IConfiguration>(cfg);

            var serviceProvider = sc.BuildServiceProvider();

            var featureManager = serviceProvider.GetRequiredService<IFeatureManager>();

            var featureState = await featureManager.IsEnabledAsync("FeatureA");

            Assert.IsFalse(featureState, "FeatureA must be disabled as it's Value is False");
        }
    }
}

Actual result:: featureManager.IsEnabledAsync("FeatureA") returns True

Root cause: ConfigurationFeatureDefinitionProvider doesn't distinguish if there is an actual False value set or value can't be parsed in the method ParseDotnetSchemaFeatureDefinition:

 61: if (!string.IsNullOrEmpty(val) && bool.TryParse(val, out bool result) && result)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions