-
Notifications
You must be signed in to change notification settings - Fork 5k
Configuration Json provider: adds empty objects to configuration #40829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configuration Json provider: adds empty objects to configuration #40829
Conversation
Tagging subscribers to this area: @maryamariyan |
5465789
to
b553663
Compare
var jsonConfigSource = new JsonConfigurationProvider(new JsonConfigurationSource()); | ||
jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)); | ||
|
||
Assert.Equal("", jsonConfigSource.Get("key")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced this is intentional, I've raised #40827
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks @alefranz.
src/libraries/Microsoft.Extensions.Configuration.Json/tests/EmptyObjectTest.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration.Json/src/JsonConfigurationFileParser.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably return ""
instead of null
for empty objects for consistency.
Thank you! |
This just broke our service rollout today, because a whole bunch of nulls started flowing through our configuration layers that didn't use to exist. If anyone else ends up here because of a bunch of NullReferenceExceptions in config code when updating from 5.0.0 => 6.0.0 of Microsoft.Extensions.Configuration.Json, the answer is just to remove any empty objects from all your config files. |
I had marked this as "breaking-change" before realizing it was 2 years old so adding a breaking change notice for 5.0 is not really going to help much. In the future we should be careful about changing the nullable behavior of our APIs. I would hope that such a change would appear as a change to the nullable annotations in the API which are now present as of 7.0 preview4, and thus it would be a signal to folks recompiling to handle null. I would still consider that "breaking" in the sense that we should document it, but allow it. I still think we could do more here. I was wondering if a debug visualizer on the configuration section might have been a signal to you debugging if you saw that the element had a null value. I wasn't sure of the proximity between your usage of the Configuration API and the API which threw the NRE and if a debug visualizer might help. |
We only hit it because 3.1 is coming to the end of its life soon, so we had to migrate. I assume others will be in this boat relatively soon as well, so documentation might still be helpful. I don't know if we have any good numbers on how many people are still on 3.1. I don't think the config section would have existed yet in the code that crashed, since it was in the middle of the configuration pipeline. It crashed inside "Load", which caught the ArgumentNullException and turned it into a InvalidDataException, so tracking it down took a fair bit of time. This behavior is unexpected, so I wouldn't have even though to look at a visualizer. The idea that |
Currently empty objects loaded via ConfigurationBuilder.AddJsonFile() are ignored.
This adds empty objects to the configuration, so that when you enumerate the children on the parent, the key will be listed.
This element is present in configuration as a null value without children.
Please note that the extension method
Exists()
will return false for this element, as "exists" is a misnomer. The actual semantic of that extension method is "not empty", as per description it is checking that the element has at least a value or children.I believe that, given the actual semantic of
Exists
, it is correct for it to be false in this scenario.Fixes: #40218