Skip to content

ConfigurationBinder source generator build fails with CS9035 if option class have Property with requried #95006

Description

@xmarshal

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>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions