Skip to content

OpenAPI is incorrectly generating documents at build-time #60808

@seesharprun

Description

@seesharprun

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When I create a new minimal Web API project using dotnet new webapi, I get build-time errors when all of these conditions are all true:

  • I'm using AOT
  • I'm using C# records with required properties
  • I'm binding configuration using the C# records

In many of my projects, I have C# records defined in a hierarchical manner so I can take advantage of the IOptions<> pattern and binding

Expected Behavior

Per the documentation, I shouldn't expect ANY build-time generation to happen at all unless I include the Microsoft.Extensions.ApiDescription.Server package from NuGet.

I explicitly do not include that package. I don't know if the package is implicitly included by the *.csproj SDK configuration property being set to Microsoft.NET.Sdk.Web.

In an ideal world, these steps should just work:

cd src/api

dotnet build

dotnet user-secrets set "CONFIGURATION:DETAILS:SOLUTION" "Example app"
dotnet user-secrets set "CONFIGURATION:DETAILS:DEPARTMENT" "Information technology"

dotnet run
GET http://localhost:5151
Accept: application/json
{
  "name": "Example",
  "description": "Application solution for IT department.",
  "version": "1.0.0",
  "status": "Healthy"
}

Steps To Reproduce

I created a minimal repo that reproduces this issue here: https://github.com/seesharprun/webapiopenaapigen

I also ran a build in GitHub Actions so you can see the build error in an isolated environment (not on my machine 😆): https://github.com/seesharprun/webapiopenaapigen/actions/runs/13723101353/job/38382977103

I can reproduce the error following these steps:

cd src/api

dotnet build

Exceptions (if any)

Restore complete (0.4s)
  Example.Api failed with 2 error(s) (0.6s)
    <redacted>\src\api\obj\Debug\net9.0\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(99,31): error CS9035: Required member 'Details.Solution' must be set in the object initializer or attribute constructor.
    <redacted>\src\api\obj\Debug\net9.0\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(99,31): error CS9035: Required member 'Details.Department' must be set in the object initializer or attribute constructor.

Build failed with 2 error(s) in 1.9s

.NET Version

9.0.200

Anything else?

I can "fix" the issue by doing one of two things:

  1. I can remove AOT from the project entirely by explicitly disabling it in the *.cspoj file with this setting:

    <PublishAot>false</PublishAot>
  2. I can update the record type to have default values:

    internal sealed class Configuration
    {
        public Details Details { get; init; } = new() { Solution = "Example", Department = "IT" };
    }
    internal sealed class Details
    {
        public string Solution { get; init; } = "Example";
    
        public string Department { get; init; } = "IT";
    }

In a perfect world, I could just disable build-time generation because I intentionally want my configuration class to not have default settings. I'd rather set the defaults using an appsettings.json file than in C# code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.ExternalThis is an issue in a component not contained in this repository. It is open for tracking purposes.NativeAOTStatus: Resolved

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions