Skip to content

ConfigurationBinder.Get binds empty IDictionary<string, string> as null #106820

@realbart

Description

@realbart

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When serializing options, I expect an empty JSON-object { } to be serialized to an empty Dictionary.

Expected Behavior

When reading an options-object using configuration.GetSection("Foo").Get<FooOptions>(); I expect a dictionary to be empty not null.

Steps To Reproduce

  1. Create a configuration
{
    "Foo" : {
        "Bar": 1,
        "Baz": {
        }
    }  
}
  1. Create an OPTIONS object
public record FooOptions
{
    public required int Bar { get; init; }
    public required IDictionary<string, string> Baz{ get; init; }
}
  1. Read it!
public void ConfigureServices(IConfiguration configuration, IServiceCollection services, TokenCredential credential)
{
    var fooOptions = configuration.GetSection("Foo").Get<FooOptions>();
    foreach ((var key, var value) in Baz) {
       // do something
    }
}

Expected behavior:

fooOptions.Baz is an empty dictionary, which can be enumerated

Actual behavior:

fooOptions.Baz is null

Exceptions (if any)

No response

.NET Version

8.0.301

Anything else?

This behavior occurs in a ASP.NET 8 project

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions