Description
ConfigurationBinder source generator generates code that doesn't compile when using recodrs with requried properties as option Property
Reproduction Steps
build this code
namespace BindGeneratorRepro;
using System.Reflection;
internal class Program
{
private static void Main(string[] args)
{
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.AddOptions();
builder.Services.AddOptions<MyOptionA>()
.BindConfiguration("myPath", options => options.BindNonPublicProperties = true);
var app = builder.Build();
app.MapGet("/version", () =>
{
var fileVersionAttribute = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>();
return Results.Text(fileVersionAttribute!.Version);
});
app.Run();
}
}
internal sealed record MyOptionA
{
public required string MyParamA { get; init; }
public required MyOptionB MyParamAB { get; init; }
}
internal sealed record MyOptionB
{
public required string MyParamB { get; init; }
}
Expected behavior
Code should compiles
Actual behavior
build files with error CS9035
BinGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(106,31,106,40): error CS9035: Required member 'MyOptionB.MyParamB' must be set in the object initializer or attribute constructor.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
<EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
</PropertyGroup>
</Project>
Description
ConfigurationBinder source generator generates code that doesn't compile when using recodrs with requried properties as option Property
Reproduction Steps
build this code
Expected behavior
Code should compiles
Actual behavior
build files with error CS9035
BinGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(106,31,106,40): error CS9035: Required member 'MyOptionB.MyParamB' must be set in the object initializer or attribute constructor.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information