Empty array in config json file results in null class property
See original GitHub issueDescribe 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:
- Created 4 years ago
- Reactions:3
- Comments:10
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 reallynull
or when it simply doesn’t exist.However, I don’t understand why empty arrays suffer from that problem.
[]
is not the same asnull
. If your code is treating those two as the same, I think that’s a bug.Filed https://github.com/dotnet/extensions/issues/3083