Skip to content

Empty objects loaded via ConfigurationBuilder.AddJsonFile() are ignored #40218

Closed
@halter73

Description

@halter73

Description

Empty objects loaded via ConfigurationBuilder.AddJsonFile() are ignored.


Repro

config.json
{
    "a": "b",
    "c": {
        "d": "e"
    },
    "f": "",
    "g": null,
    "h": {},
    "i": {
        "k": {}
    } 
}
Program.cs
using System;
using Microsoft.Extensions.Configuration;

namespace EmptyConfigSection
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                .AddJsonFile("config.json")
                .Build();

            Console.WriteLine("{");

            foreach (var child in config.GetChildren())
            {
                PrintConfigSection(child, padding: "    ");
            }

            Console.WriteLine("}");
        }

        static void PrintConfigSection(IConfigurationSection configSection, string padding)
        {
            if (configSection.Value is null)
            {
                Console.WriteLine($@"{padding}""{configSection.Key}"": {{");

                foreach (var child in configSection.GetChildren())
                {
                    PrintConfigSection(child, $"    {padding}");
                }

                Console.WriteLine($"{padding}}},");
            }
            else
            {
                Console.WriteLine($@"{padding}""{configSection.Key}"": ""{configSection.Value}"",");
            }
        }
    }
}
EmptyConfigSection.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0-preview.7.20364.11" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="config.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

</Project>

Expected output

{
    "a": "b",
    "c": {
        "d": "e",
    },
    "f": "",
    "g": "",
    "h": {
    },
    "i": {
        "k": {
        },
    },
}

Actual output

{
    "a": "b",
    "c": {
        "d": "e",
    },
    "f": "",
    "g": "",
}

"h", "i" and "k" are not present in the actual output even though they should be.

The top-level call to IConfiguration.GetChildren() in Program.Main() should return an IEnumerable containing 6 top-level IConfigurationSections: "a", "c", "f", "g", "h" and "k". Instead it returns 4 top-level IConfiguration sections omitting both "h" and "i".

The repro program never even makes a call to GetChildren() that should return the "k" subsection, because it never sees "k"'s parent "i" in the first place.

Configuration

F:\dev> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100-preview.7.20330.3
 Commit:    eeb77e1a55

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.20161
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   F:\dev\aspnet\AspNetCore\.dotnet\sdk\5.0.100-preview.7.20330.3\

Host (useful for support):
  Version: 5.0.0-rc.1.20370.4
  Commit:  0e0e648770

I do not think this issue is specific to my configuration however.

Regression?

  • I don't think this is a regression, but I cannot say for sure.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions