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

Update summary for ConfigurationBinder.GetValue<T>(...) to clarify default value return #9639

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MashalMohammed
Copy link

@MashalMohammed MashalMohammed commented Feb 25, 2024

Summary

This PR clarifies return of default value in ConfigurationBinder.GetValue<T>(...).

ConfigurationBinder.GetValue<T>(...) returns null if the specified key is not found in the IConfiguration, which is not apparent or documented, and user might expect an error, incorrectly.
Sibling variant methods has a default value param at the least.

namespace Microsoft.Extensions.Configuration
{
    /// <summary>
    /// Static helper class that allows binding strongly typed objects to configuration values.
    /// </summary>
    public static class [ConfigurationBinder]
    {
    
        ...
    
        /// <summary>
        /// Extracts the value with the specified key and converts it to type T.
        /// </summary>
        /// <typeparam name="T">The type to convert the value to.</typeparam>
        /// <param name="configuration">The configuration.</param>
        /// <param name="key">The key of the configuration section's value to convert.</param>
        /// <param name="defaultValue">The default value to use if no value is found.</param>
        /// <returns>The converted value.</returns>
        public static T? GetValue<T>(this IConfiguration configuration, string key, T defaultValue)
        {
            return (T?)GetValue(configuration, typeof(T), key, defaultValue);
        }

https://source.dot.net/#Microsoft.Extensions.Configuration.Binder/ConfigurationBinder.cs,232ab46b757d72f9

I've just mentioned that the method could return a default if the key is not present.

Fixes #9638.

@MashalMohammed MashalMohammed requested a review from a team as a code owner February 25, 2024 07:01
@MashalMohammed MashalMohammed changed the title update summary for ConfigurationBinder.GetValue<T>(...) to clarify default value return Update summary for ConfigurationBinder.GetValue<T>(...) to clarify default value return Feb 25, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 25, 2024
Copy link

Learn Build status updates of commit 44dbde8:

✅ Validation status: passed

File Status Preview URL Details
xml/Microsoft.Extensions.Configuration/ConfigurationBinder.xml ✅Succeeded View

For more details, please refer to the build report.

For any questions, please:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-Configuration community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unclear doc or signature for ConfigurationBinder.GetValue<T>(...) which returns a default
1 participant