Skip to content

Empty array in YAML is emitted as nullΒ #81

@jsholzer

Description

@jsholzer

I noticed that if define a YAML configuration with a property that is set to an empty array ([]), then when I ultimately call IConfiguration.Bind to bind the config data to a class, that property is deserialized as null.

It is possible to work around this issue, by either changing the YAML to use [null] or setting up the property in the class to convert null values to an empty collection in the setter. However, I would expect that an explicitly defined empty array would be emitted as an empty collection (list, array, etc), rather than null.

For example, if I have the following YAML config and class:

# appsettings.yml
MyApp:
  SomeList: []
public class MyAppConfig
{
    public List<string> SomeList {get; set;}
}

And then bind it using something like this, then the resulting value of SomeList will be null, rather than an empty list

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddYamlFile("appsettings.yml", optional: false);

WebApplication app = builder.Build();

var config = new MyAppConfig();
app.Configuration.Bind("MyApp", config);

If I change the definition of SomeList to be SomeList: [null], then it will be emitted/bound as an empty list.

This feels like a bug, but it's more of an inconvenience than anything, since it can be worked around.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions