-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Ensure Bind can handle null from GetSection #92384
Conversation
IConfiguration instances may return a null value from GetSection. We were not handling this and would throw a NullReferenceException.
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsFixes #92213 IConfiguration instances may return a null value from GetSection. We were not handling this and would throw a NullReferenceException.
|
@@ -298,6 +298,11 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig | |||
return; | |||
} | |||
|
|||
if (config == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously the entire block below was wrapped in a null check - 35f043f#diff-2e6f75e1d4f577e1c8c1b6a32d158ce7812a38ad8fcd3b4d762157b92c03e890L308-R315
Since this method already has multiple returns, I find it more readable to early-exit rather than count curly-brackets/indentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good to me.
Thank you! |
...libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added minor comment/suggestion, LGTM otherwise. Thanks for fixing this.
...libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
Outdated
Show resolved
Hide resolved
@RussKie - if you wanted to workaround this you can register a mock section as the default return value for mockConfSection.Setup(m => m.GetSection(It.IsAny<string>())).Returns(emptySection.Object); That would ensure that GetSection doesn't return |
👍 |
...libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Show resolved
Hide resolved
Remaining legs are outer loop build incorrectly triggered for all PRs |
/backport to release/8.0 |
Started backporting to release/8.0: https://github.com/dotnet/runtime/actions/runs/6268341143 |
/backport to release/8.0-rc2 |
Started backporting to release/8.0-rc2: https://github.com/dotnet/runtime/actions/runs/6275041479 |
Fixes #92213
IConfiguration instances may return a null value from GetSection. We were not handling this and would throw a NullReferenceException.