Skip to content

Commit

Permalink
Fix #51171: Eager validation of named options (#55922)
Browse files Browse the repository at this point in the history
  • Loading branch information
xakep139 authored Jul 20, 2021
1 parent 85cf53d commit 6b09e32
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class OptionsBuilderExtensions
{
// This adds an action that resolves the options value to force evaluation
// We don't care about the result as duplicates are not important
vo.Validators[typeof(TOptions)] = () => options.Get(optionsBuilder.Name);
vo.Validators[(typeof(TOptions), optionsBuilder.Name)] = () => options.Get(optionsBuilder.Name);
});

return optionsBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Extensions.DependencyInjection
{
internal sealed class ValidationHostedService : IHostedService
{
private readonly IDictionary<Type, Action> _validators;
private readonly IDictionary<(Type, string), Action> _validators;

public ValidationHostedService(IOptions<ValidatorOptions> validatorOptions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.Extensions.DependencyInjection
{
internal sealed class ValidatorOptions
{
// Maps each options type to a method that forces its evaluation, e.g. IOptionsMonitor<TOptions>.Get(name)
public IDictionary<Type, Action> Validators { get; } = new Dictionary<Type, Action>();
// Maps each pair of a) options type and b) options name to a method that forces its evaluation, e.g. IOptionsMonitor<TOptions>.Get(name)
public IDictionary<(Type optionsType, string optionsName), Action> Validators { get; } = new Dictionary<(Type, string), Action>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ public async Task ValidateOnStart_NamedOptions_ValidatesFailureOnStart()
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[ActiveIssue("https://github.com/dotnet/runtime/issues/52114", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
private async Task ValidateOnStart_AddOptionsMultipleTimesForSameType_LastOneGetsTriggered()
private async Task ValidateOnStart_AddNamedOptionsMultipleTimesForSameType_BothGetTriggered()
{
bool firstOptionsBuilderTriggered = false;
bool secondOptionsBuilderTriggered = false;
var hostBuilder = CreateHostBuilder(services =>
{
services.AddOptions<ComplexOptions>("bad_configuration1")
.Configure(o => o.Boolean = false)
.Configure(o => o.Boolean = true)
.Validate(o =>
{
firstOptionsBuilderTriggered = true;
Expand Down Expand Up @@ -175,7 +175,7 @@ private async Task ValidateOnStart_AddOptionsMultipleTimesForSameType_LastOneGet
ValidateFailure<ComplexOptions>(error, 2, "Boolean", "Integer");
}

Assert.False(firstOptionsBuilderTriggered);
Assert.True(firstOptionsBuilderTriggered);
Assert.True(secondOptionsBuilderTriggered);
}

Expand Down

0 comments on commit 6b09e32

Please sign in to comment.