-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Behavior changed during binding to collections in Net 8 #97558
Comments
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsDescriptionDuplicated properties when deserialize array from appsetting.json only with .Net 8 Configurationon the appsetting.json i have an object with this structure: "ArrayObject": [
{
"Elements":
{
"Type": "xxxx",
"BaseUrl": "xxxxxxxx",
"one": "",
"xxxx": ""
}
}
] There are a stranger behavior only with .Net 8 var builder = WebApplication.CreateBuilder(args);
var result = builder.Configuration.GetSection("ArrayObject").Get<List<ArrayObject>>();
the result this case will be Regression?As you see Elements should be null and empty but with Net 8 no it's take the number of properties on the elements objects. Other information
|
@bbenameur - can you post a complete repro? The code above doesn't compile since it doesn't define |
Hello @eerhardt |
Confirmed the change in behavior, here's a simplified repro in console app. Having a look for the cause to determine if it's intentional or not. |
Yes i confirm that the behavior is changed |
@ericstj Am I in the right group dotnet/runtime ? |
Yes this is the right group and area. I'm updating the issue as I understand more. Thank you for the report. This is the cause: e7e9dbd#diff-2e6f75e1d4f577e1c8c1b6a32d158ce7812a38ad8fcd3b4d762157b92c03e890R420 I read through that PR and didn't see why we were adding another case here. Moreover - we'll only create the instance and never bind any of it's properties which seems wrong. If I change the type to have a constructor, then it will fail: public class Element
{
public Element(string type) { Type = type; }
public string Type { get; set; }
} var result = config.GetSection("ArrayObject").Get<List<ArrayObject>>(o => o.ErrorOnUnknownConfiguration = true);
This is definitely a regression that we'll look into fixing. @bbenameur - is this blocking you? Seems like your JSON might have been set up in a way that didn't match your objects and you weren't noticing that until now. Can you fix either the JSON or your type shape to make things work correctly? |
Thanks @ericstj for your quick response, |
Looks we fixed this in 9.0 #96737. We were watching to see if more reports on the issue before porting it to servicing. I'll confirm first the fix address this issue and then will port it to servicing. |
Update: #96737 doesn't address this issue. I'll look more here at the right fix. |
I originally misidentified the regressing change, it was actually #86485 which was fixing Configuration Binding a dictionary with a key that has empty value skips the key as well - that makes more sense. It still seems wrong to create an instance when operating on a config section with a value, that value cannot be directly assigned, and there are no child sections. Probably the fix for the above didn't scope down the config input enough - it handled no-child values, but didn't check for no value. Maybe we only do this if the config.value is null as well. |
The behavior is ok only if the element class has a constructor and without o => o.ErrorOnUnknownConfiguration = true public class Element
{
public Element(string type) { Type = type; }
public string Type { get; set; }
} |
This is expected because the binder will not be able to create instance of the |
Hello @tarekgh |
@bbenameur For sure we are going to fix it in 9.0. We'll consider porting the fix back 8.0 if the changes are not risky and scoped. The fix for this is going to span the source generator too which I am currently looking at. |
This is now fixed in 9.0. We'll watch if we'll get more reports and then consider porting the fix to 8.0. |
Description
Duplicated properties when deserialize array from appsetting.json only with .Net 8
Configuration
on the appsetting.json i have an object with this structure:
There are a stranger behavior only with .Net 8
using
Regression?
As you see Elements should be null and empty but with Net 8 no it's take the number of properties on the elements objects.
Other information
It's ok with Net 6 see image

The text was updated successfully, but these errors were encountered: