Skip to content
Closed
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 @@ -18,6 +18,7 @@ namespace Microsoft.FeatureManagement
sealed class ConfigurationFeatureDefinitionProvider : IFeatureDefinitionProvider, IDisposable
{
private const string FeatureFiltersSectionName = "EnabledFor";
private static readonly string[] SectionSeparator = new string[] { ConfigurationPath.KeyDelimiter };
private readonly IConfiguration _configuration;
private readonly ConcurrentDictionary<string, FeatureDefinition> _definitions;
private IDisposable _changeSubscription;
Expand Down Expand Up @@ -83,8 +84,39 @@ public async IAsyncEnumerable<FeatureDefinition> GetAllFeatureDefinitionsAsync()

private FeatureDefinition ReadFeatureDefinition(string featureName)
{
IConfigurationSection configuration = GetFeatureDefinitionSections()
.FirstOrDefault(section => section.Key.Equals(featureName, StringComparison.OrdinalIgnoreCase));
IConfigurationSection configuration;

if (!featureName.Contains(ConfigurationPath.KeyDelimiter))
{
configuration = GetFeatureDefinitionSections()
.FirstOrDefault(section => section.Key.Equals(featureName, StringComparison.OrdinalIgnoreCase));
}
else
{
//
// Feature names with configuration path delimiters require traversing children sections for resolution
IEnumerable<string> sectionNames = featureName.Split(SectionSeparator, StringSplitOptions.None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we define SectionSeparator as a string instead of string[]? Another option is to directly use ConfigurationPath.KeyDelimiter because this is a special delimiter that's unlikely to change in .NET.

Copy link
Member Author

@jimmyca15 jimmyca15 Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just stored it that way since Split accepts string[] and ConfigurationPath.KeyDelimiter is a string instead of a char. It's an optimization instead of creating a new string array every time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, Split also accepts string delimiter.


IEnumerable<IConfigurationSection> sections;

IConfigurationSection section = null;

foreach (string sectionName in sectionNames)
{
sections = section == null ?
GetFeatureDefinitionSections() :
section.GetChildren();
Comment on lines +106 to +108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

section == null will only be true for the first iteration in the foreach loop. Instead of doing this check inside the loop every time, can we initialize sections to GetFeatureDefinitionSections() before the loop?


section = sections.FirstOrDefault(section => section.Key.Equals(sectionName, StringComparison.OrdinalIgnoreCase));

if (section == null)
{
break;
}
}

configuration = section;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If featureName is Feature:With:Colons, after the foreach loop ends, we'll set configuration to the section named Colons . Then we call ReadFeatureDefinition(configuration), and that method will return the FeatureDefinition object where FeatureDefinition.Name will be set to Colons instead of Feature:With:Colons (code).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This made me realize a bigger problem which is how this works with GetFeatureDefinitionAsync. We may not be able to handle that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetFeatureDefinitionAsync can't detect features that have :. That's a glaring hole in supporting :. We may just want to block : because of that.

}

if (configuration == null)
{
Expand Down
41 changes: 41 additions & 0 deletions tests/Tests.FeatureManagement/FeatureManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class FeatureManagement
private const string OffFeature = "OffFeature";
private const string ConditionalFeature = "ConditionalFeature";
private const string ContextualFeature = "ContextualFeature";
private const string FeatureWithColons = "Feature:With:Colons";

[Fact]
public async Task ReadsConfiguration()
Expand Down Expand Up @@ -615,6 +616,46 @@ public async Task ThreadsafeSnapshot()
}
}

[Fact]
public async Task AllowsColon()
{
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

var services = new ServiceCollection();

services
.AddSingleton(config)
.AddFeatureManagement()
.AddFeatureFilter<TestFilter>();

ServiceProvider serviceProvider = services.BuildServiceProvider();

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

IEnumerable<IFeatureFilterMetadata> featureFilters = serviceProvider.GetRequiredService<IEnumerable<IFeatureFilterMetadata>>();

//
// Sync filter
TestFilter testFeatureFilter = (TestFilter)featureFilters.First(f => f is TestFilter);

bool called = false;

testFeatureFilter.Callback = (evaluationContext) =>
{
called = true;

Assert.Equal("V1", evaluationContext.Parameters["P1"]);

Assert.Equal(FeatureWithColons, evaluationContext.FeatureName);

return Task.FromResult(true);
};

await featureManager.IsEnabledAsync(FeatureWithColons);

Assert.True(called);
}

private static void DisableEndpointRouting(MvcOptions options)
{
#if NET5_0 || NETCOREAPP3_1
Expand Down
10 changes: 10 additions & 0 deletions tests/Tests.FeatureManagement/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
}
}
]
},
"Feature:With:Colons": {
"EnabledFor": [
{
"Name": "Test",
"Parameters": {
"P1": "V1"
}
}
]
}
}
}