From: Taylor Southwick Date: Tue, 29 Sep 2020 20:59:26 +0000 (-0700) Subject: Check for null in OptionsFactory (#41047) X-Git-Tag: submit/tizen/20210909.063632~5277 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f4094bfaaa7e2fa3fd5b765949adb464a9bd4d73;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Check for null in OptionsFactory (#41047) If an IValidateOptions returns null, then an error occurs that can be difficult to track down. This change checks for null before checking if it failed. --- diff --git a/src/libraries/Microsoft.Extensions.Options/src/OptionsFactory.cs b/src/libraries/Microsoft.Extensions.Options/src/OptionsFactory.cs index dec163c..35c383d 100644 --- a/src/libraries/Microsoft.Extensions.Options/src/OptionsFactory.cs +++ b/src/libraries/Microsoft.Extensions.Options/src/OptionsFactory.cs @@ -68,7 +68,7 @@ namespace Microsoft.Extensions.Options foreach (IValidateOptions validate in _validations) { ValidateOptionsResult result = validate.Validate(name, options); - if (result.Failed) + if (result is not null && result.Failed) { failures.AddRange(result.Failures); } diff --git a/src/libraries/Microsoft.Extensions.Options/tests/Microsoft.Extensions.Options.Tests/OptionsFactoryTests.cs b/src/libraries/Microsoft.Extensions.Options/tests/Microsoft.Extensions.Options.Tests/OptionsFactoryTests.cs index e49c65e..5c09fe9 100644 --- a/src/libraries/Microsoft.Extensions.Options/tests/Microsoft.Extensions.Options.Tests/OptionsFactoryTests.cs +++ b/src/libraries/Microsoft.Extensions.Options/tests/Microsoft.Extensions.Options.Tests/OptionsFactoryTests.cs @@ -33,8 +33,8 @@ namespace Microsoft.Extensions.Options.Tests var factory = sp.GetRequiredService>(); Assert.Equal("UP", factory.Create("UP").Message); Assert.Equal("up", factory.Create("up").Message); - } - + } + [Fact] public void CanConfigureAllOptions() { @@ -199,6 +199,14 @@ namespace Microsoft.Extensions.Options.Tests } } + public class FakeOptionsValidationNull : IValidateOptions + { + public ValidateOptionsResult Validate(string name, FakeOptions options) + { + return null; + } + } + [Fact] public void CanValidateOptionsWithConfigureOptions() { @@ -212,6 +220,17 @@ namespace Microsoft.Extensions.Options.Tests Assert.Equal("Hello world", message); } + [Fact] + public void ValidateOptionsChecksNullWithConfigureOptions() + { + var factory = new ServiceCollection() + .ConfigureOptions() + .BuildServiceProvider() + .GetRequiredService>(); + + Assert.NotNull(factory.Create(Options.DefaultName)); + } + public class UberSetup : IConfigureNamedOptions , IConfigureNamedOptions @@ -221,12 +240,12 @@ namespace Microsoft.Extensions.Options.Tests , IValidateOptions { public void Configure(string name, FakeOptions options) - => options.Message += "["+name; + => options.Message += "[" + name; public void Configure(FakeOptions options) => Configure(Options.DefaultName, options); public void Configure(string name, FakeOptions2 options) - => options.Message += "[["+name; + => options.Message += "[[" + name; public void Configure(FakeOptions2 options) => Configure(Options.DefaultName, options);