question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty array in config json file results in null class property

See original GitHub issue

Describe the bug

Empty array in config json file results in null class property

To Reproduce

appsettings.json

{
  "nullValue": null,
  "anEmptyArray": [],
  "ridiculousWorkaround": ["this string has to not be empty"]
}

Startup.cs (ConfigureServices method)

var nullValue = Configuration.GetSection("nullValue").Get<IEnumerable<string>>();
var values = Configuration.GetSection("anEmptyArray").Get<IEnumerable<string>>();
var workaround = Configuration.GetSection("ridiculousWorkaround").Get<IEnumerable<string>>();

// nullValue is null
// values is null
// workaround is List<string> length 1

Expected behavior

in the above example, ‘nullValue’ would be null, ‘values’ should be an empty enumerable, and ‘workaround’ should be an enumerable with 1 value.

Additional context

There was an issue filed about this that was closed, but the issue was not fixed. https://github.com/aspnet/Configuration/issues/788

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:10

github_iconTop GitHub Comments

3reactions
andre-ss6commented, Jun 13, 2019

At least it should work with empty arrays. From what I read on the original issue, the problem is that you check for a key and if it doesn’t exist, null is returned, thus you don’t know when it’s really null or when it simply doesn’t exist.

However, I don’t understand why empty arrays suffer from that problem. [] is not the same as null. If your code is treating those two as the same, I think that’s a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Empty array in config json file results in null class property
Describe the bug Empty array in config json file results in null class property To Reproduce appsettings.json { "nullValue": null, ...
Read more >
Null property when binding to empty array in appsettings.json
I stumbled across an issue when using the Options pattern where model properties are being set to null when bound to an empty...
Read more >
Handling JSON null and empty arrays and objects
Null values ​​ JSON has a special value called null which can be set on any type of data including arrays, objects, number...
Read more >
Null property when binding to empty array in appsettings.json ...
this way i don't have to check for null or not configured values each time i'm using my configuration object. croxy 4006. score:0....
Read more >
Use empty string, null or remove empty property in API ...
The first thing to bear in mind is that applications at their edges are not object-oriented (nor functional if programming in that paradigm)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found