Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #51171: Eager validation of named options #55922

Merged
merged 1 commit into from
Jul 20, 2021
Merged
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 @@ -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