Repository navigation
Description
Background and Motivation
When defining a configuration that has some defaulted options that are a List<T> or an array type we might want to have the ability to provide a default list, but completely replaces the enumerable's contents with what is in the configuration file.
Proposed API
namespace Microsoft.Extensions.Configuration
{
public class BinderOptions
{
public bool BindNonPublicProperties { get; set; }
+ public bool OverrideLists { get; set; }
}
}Usage Examples
Expanding upon my original repo as a base
var config = new ConfigurationBuilder().AddNewtonsoftJsonStream(new MemoryStream(Encoding.ASCII.GetBytes("{ \"Example\": [ \"Item 2\" ] }"))).Build();
var obj = config.Get<ConfigType>(options => options.OverrideLists = true);Alternative Designs
This gives a similar action to Newtonsoft.JSON's ObjectCreationHandling option with the value of Replace which when handling an Object when deserializing to a class, it uses a new class with no contents at all.
Original issue
Description
Example Repo: https://github.com/Cazzar/ConfigurationArrayTest
To show the issue itself, simply running dotnet run should work
The issue itself seems to occur that if you have an initialization for an IEnumerable instead of having the option to Replace the contents on deserialization, it will append to the contents, making it impossible to use a strongly typed configuration instance with defaults in the case that they are not provided.
Testing this, it seems to show both a pattern within both System.Text.Json and the Newtonsoft.Json versions of configuration loading.
Configuration
- Windows 10 Build 19042.685 NET 5.0.1 x64
- Doesn't seem to be configuration specific.
Other information
Example output from my repo above:
Newtonsoft.JSON
Item Count: 2
Items in array: Item 1, Item 2
System.Net.Json
Item Count: 2
Items in array: Item 1, Item 2
In this case, I would expect only Item 2 to exist in the arrays, effectively overwriting the bound object with the contents of the configuration.
From further speculation, this might not be specifically bound to the input type but by the Binding options not having the option to replace
An initial thought would be for this using Add without clearing the collection first:
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Lines 423 to 447 in 796cbee
| private static void BindCollection(object collection, Type collectionType, IConfiguration config, BinderOptions options) | |
| { | |
| // ICollection<T> is guaranteed to have exactly one parameter | |
| Type itemType = collectionType.GenericTypeArguments[0]; | |
| MethodInfo addMethod = collectionType.GetMethod("Add", DeclaredOnlyLookup); | |
| foreach (IConfigurationSection section in config.GetChildren()) | |
| { | |
| try | |
| { | |
| object item = BindInstance( | |
| type: itemType, | |
| instance: null, | |
| config: section, | |
| options: options); | |
| if (item != null) | |
| { | |
| addMethod.Invoke(collection, new[] { item }); | |
| } | |
| } | |
| catch | |
| { | |
| } | |
| } | |
| } |
Activity
[-]Microsoft.Extensions.Configuration JSON should [/-][+]Microsoft.Extensions.Configuration App binding adding to the initialized Collections, not replacing [/+]CodeBlanch commentedon Jan 15, 2021
@Cazzar Funny I just ran into this too. It seems to be working as designed given there are unit tests for the behavior, for example:
runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationCollectionBindingTests.cs
Lines 305 to 332 in e3247ad
What I ran into is in the case of
IReadOnlyCollection......configuration is completely ignored because you can't add to an existing
IReadOnlyCollection.My expectation was that "ReadOnlyValues" would be completely replaced if something was specified in the configuration.
Cazzar commentedon Jan 15, 2021
@CodeBlanch Interesting, even if it was a binding option that might be a reasonable solution, since if you were to use raw JSON parsing, it is possible, at least with Newtonsoft.JSON that you can replace instead of update.
I've got a potential workaround idea that I will try later today and update here for the solution.
Given that it is an expected pattern, breaking the current API would not be a good idea, I wouldn't be against some sort of binding options.
safern commentedon Jan 29, 2021
@Cazzar this definitely looks like by design and it would be a breaking change to "fix" this. What I might suggest if we wanted to support this scenario would be adding an option to BindingOptions that is something like:
OverrideListsor something like that, that would trigger the behavior you're suggesting.Anyway, were you able to find a workaround for this? If not I can help finding one. I think we might be able to close this issue?
Cazzar commentedon Jan 30, 2021
@safern I, unfortunately, couldn't find something that worked well enough for my use-case (though it may also relate to the need of the configuration being serialised back in place which was a dirty bit of code, to begin with). Since I was using it with
IOptions, I had a fair bit to try including late-initializing the properties and ended up moving to the previous setup we had internally.Given it does look like intended functionality as I mentioned in #46988 (comment) an option within BindingOptions would probably be a good idea in the long run.
safern commentedon Feb 1, 2021
Would you mind submitting a formal API proposal to add that BindingOption as part of this or a new issue?
Here are the instructions on how to do that: https://github.com/dotnet/runtime/blob/master/docs/project/api-review-process.md
Cazzar commentedon Feb 2, 2021
Because I would have just referenced this issue, I have updated the above description based off the process @safern
safern commentedon Feb 2, 2021
Thank you, @Cazzar. Based on the proposed API, would it be reasonable that the flag is named something like:
UseDefaultListsor something that has to be set to true explicitly to trigger this behavior? Currently overriding the list is the default behavior, so I would like a flag that is explicit about turning on a new feature vs turning off an old behavior, does that make sense?CodeBlanch commentedon Feb 2, 2021
@safern The behavior right now is to add to the list, if possible, otherwise it does nothing (the readonly case). The behavior we want to enable is to override whatever is there.
safern commentedon Feb 2, 2021
Sorry I mixed up things. You're right. The proposal looks good.
terrajobst commentedon Feb 9, 2021
Clear()method.IListand is settable, preferring the setter seems reasonable.