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

Unable to bind Dictionary with key type "Enum" when key string case is different #71185

Closed
mortifactor opened this issue Jun 23, 2022 · 1 comment · Fixed by #71926
Closed
Labels
area-Extensions-Configuration bug help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@mortifactor
Copy link

Description

Hello.
Since ASP.NET Core documentation says:

Configuration keys: Are case-insensitive. For example, `ConnectionString` and `connectionstring` are treated as equivalent keys.

https://github.com/dotnet/AspNetCore.Docs/blob/c875045a940e6ce3d24c24573309ff4be71d4fde/aspnetcore/fundamentals/configuration/index.md?plain=1#L404

I suppose that enum keys should be handled the same way as strings.
But at the moment ConfigurationBinder throws an error as it uses "Enum.Parse" method:

object key = keyTypeIsEnum ? Enum.Parse(keyType, child.Key) : child.Key;

Reproduction Steps

Create settings class:

public class TestConf : Dictionary<TestEnum, string>
{
}

public enum TestEnum
{
    Test1,
    Test2,
}

Modify appsettings.json:

"TestConf": {
  "Test1": "foo",
  "test2": "bar"
}

Register a configuration in Program.cs:

builder.Services.Configure<TestConf>(builder.Configuration.GetSection(nameof(TestConf)));

And finally try somewhere to get options value:

[HttpGet("test")]
public async Task<IActionResult> TestAppsettingsForIssue([FromServices] IOptionsSnapshot<TestConf> testConf)
{
    var t = testConf.Value;
    return Ok();
}

I am not sure if this is a bug or intentional behavior.

Expected behavior

Since ASP.NET Core documentation says Configuration keys are case-insensitive it should not throw an error.

Actual behavior

It throws an error System.ArgumentException: Requested value 'test2' was not found.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 23, 2022
@ghost
Copy link

ghost commented Jun 23, 2022

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Hello.
Since ASP.NET Core documentation says:

Configuration keys: Are case-insensitive. For example, `ConnectionString` and `connectionstring` are treated as equivalent keys.

https://github.com/dotnet/AspNetCore.Docs/blob/c875045a940e6ce3d24c24573309ff4be71d4fde/aspnetcore/fundamentals/configuration/index.md?plain=1#L404

I suppose that enum keys should be handled the same way as strings.
But at the moment ConfigurationBinder throws an error as it uses "Enum.Parse" method:

object key = keyTypeIsEnum ? Enum.Parse(keyType, child.Key) : child.Key;

Reproduction Steps

Create settings class:

public class TestConf : Dictionary<TestEnum, string>
{
}

public enum TestEnum
{
    Test1,
    Test2,
}

Modify appsettings.json:

"TestConf": {
  "Test1": "foo",
  "test2": "bar"
}

Register a configuration in Program.cs:

builder.Services.Configure<TestConf>(builder.Configuration.GetSection(nameof(TestConf)));

And finally try somewhere to get options value:

[HttpGet("test")]
public async Task<IActionResult> TestAppsettingsForIssue([FromServices] IOptionsSnapshot<TestConf> testConf)
{
    var t = testConf.Value;
    return Ok();
}

I am not sure if this is a bug or intentional behavior.

Expected behavior

Since ASP.NET Core documentation says Configuration keys are case-insensitive it should not throw an error.

Actual behavior

It throws an error System.ArgumentException: Requested value 'test2' was not found.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: mortifactor
Assignees: -
Labels:

area-Extensions-Configuration

Milestone: -

@maryamariyan maryamariyan added bug help wanted [up-for-grabs] Good issue for external contributors and removed untriaged New issue has not been triaged by the area owner labels Jul 5, 2022
@maryamariyan maryamariyan added this to the Future milestone Jul 5, 2022
turnyanszki added a commit to turnyanszki/runtime that referenced this issue Jul 11, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 13, 2022
eerhardt pushed a commit that referenced this issue Aug 8, 2022
…s different (#71926)

Use ignoreCase=true when parsing enum values.

Fix #71185
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 8, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Configuration bug help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants