-
Notifications
You must be signed in to change notification settings - Fork 851
Description
Description
We have Web Application with configuration supplied by an appsettings.json file. The configuration includes a collection of a custom type with nullable values. Prior to upgrading to .NET 10, the collection included instances with null entries in the nullable fields. After upgrading to .NET 10, any entry with a null field is missing from the collection.
Reproduction Steps
I created a basic Web Application in Visual Studio. The Program.cs includes:
var builder = WebApplication.CreateBuilder(args);
var options = builder.Configuration.GetSection("CustomOptions").Get<Options>();
builder.Services.AddControllers();
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
public record Options
{
public string? EnableFeatureX { get; init; }
public IEnumerable<User> Users { get; init; } = [];
}
public record User
(
int Id,
string? Name
);
and the appsettings file includes:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"CustomOptions": {
"EnableFeatureX": null,
"Users": [
{
"Id": 1,
"Name": "Noah"
},
{
"Id": 2,
"Name": null
}
]
}
}
Expected behavior
I expect to see that the "options" object when run has two entries in the Users ienumerable, one with Id: 1, Name: "Noah" and one with Id: 2, Name: null
Actual behavior
The "options" object only has one entry, Id: 1, Name: "Noah"
Regression?
This works as I expect in .NET 9. It does not work in .NET 10.
Known Workarounds
If you change the definition of "User" from a Constructor based record to one with explicit properties, expected behavior is restored:
public record User
{
public int Id { get; init; }
public string? Name { get; init; }
}
Configuration
net10.0
Visual Studio 26 (18.2)
Windows 11
Other information
.NET Fiddle .NET 9 https://dotnetfiddle.net/4bYI7v
.NET 10 https://dotnetfiddle.net/2vLnXU