Skip to content

Commit

Permalink
Prevent adding PlaceholderResolver multiple times, which breaks the E…
Browse files Browse the repository at this point in the history
…NV actuator (#1276)
  • Loading branch information
bart-vmware authored Apr 12, 2024
1 parent 2c43284 commit 7121e8f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,18 @@ public static IConfigurationBuilder AddPlaceholderResolver(this IConfigurationBu
ArgumentGuard.NotNull(builder);
ArgumentGuard.NotNull(loggerFactory);

if (builder is IConfigurationRoot configuration)
if (!builder.Sources.Any(source => source is PlaceholderResolverSource))
{
builder.Add(new PlaceholderResolverSource(configuration, loggerFactory));
}
else
{
var resolver = new PlaceholderResolverSource(builder.Sources, loggerFactory);
builder.Sources.Clear();
builder.Add(resolver);
if (builder is IConfigurationRoot configuration)
{
builder.Add(new PlaceholderResolverSource(configuration, loggerFactory));
}
else
{
var resolver = new PlaceholderResolverSource(builder.Sources, loggerFactory);
builder.Sources.Clear();
builder.Add(resolver);
}
}

return builder;
Expand Down Expand Up @@ -102,6 +105,11 @@ public static IConfiguration AddPlaceholderResolver(this IConfiguration configur
throw new InvalidOperationException($"Configuration must implement '{typeof(IConfigurationRoot)}'.");
}

if (root.Providers.Any(provider => provider is IPlaceholderResolverProvider))
{
return configuration;
}

return new ConfigurationRoot(new List<IConfigurationProvider>
{
new PlaceholderResolverProvider(new List<IConfigurationProvider>(root.Providers), loggerFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using FluentAssertions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Steeltoe.Common.Utils.IO;
Expand All @@ -22,6 +23,21 @@ public void AddPlaceholderResolver_AddsPlaceholderResolverSourceToList()
Assert.NotNull(placeholderSource);
}

[Fact]
public void AddPlaceholderResolver_NoDuplicates()
{
var configurationBuilder = new ConfigurationBuilder();

configurationBuilder.AddPlaceholderResolver();
configurationBuilder.AddPlaceholderResolver();
configurationBuilder.AddPlaceholderResolver();

PlaceholderResolverSource? source = configurationBuilder.Sources.OfType<PlaceholderResolverSource>().SingleOrDefault();
Assert.NotNull(source);
Assert.NotNull(source.Sources);
Assert.Empty(source.Sources);
}

[Fact]
public void AddPlaceholderResolver_CreatesProvider()
{
Expand Down Expand Up @@ -251,4 +267,18 @@ public void AddPlaceholderResolver_WithConfiguration_ReturnsNewConfiguration()
Assert.Equal("notfound", config2["key3"]);
Assert.Equal("${nokey}", config2["key4"]);
}

[Fact]
public void AddPlaceholderResolver_WithConfiguration_NoDuplicates()
{
IConfigurationRoot configurationRoot = new ConfigurationBuilder().Build();

IConfiguration newConfiguration = configurationRoot.AddPlaceholderResolver().AddPlaceholderResolver().AddPlaceholderResolver();

ConfigurationRoot newConfigurationRoot = newConfiguration.Should().BeOfType<ConfigurationRoot>().Which;
newConfigurationRoot.Providers.Should().HaveCount(1);

PlaceholderResolverProvider? provider = newConfigurationRoot.Providers.Single().Should().BeOfType<PlaceholderResolverProvider>().Which;
provider.Providers.Should().BeEmpty();
}
}

0 comments on commit 7121e8f

Please sign in to comment.