Skip to content

Microsoft.Extensions.ApiDescription.Server ignores minimal API endpoints #43391

@bachratyg

Description

@bachratyg

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using the Microsoft.Extensions.ApiDescription.Server package to generate openapi spec at build minimal routes are not present in the output.

ApiDescription.Server will stop the host right after HostBuilder.Build (at https://github.com/dotnet/runtime/blob/v6.0.8/src/libraries/Microsoft.Extensions.HostFactoryResolver/src/HostFactoryResolver.cs#L344). However minimal endpoints have not been added to the host at this point.

Expected Behavior

Same (equivalent) output from ApiDescription that would be emitted by Swagger at runtime

Steps To Reproduce

a.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <OpenApiGenerateDocumentsOnBuild>true</OpenApiGenerateDocumentsOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
    <PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="6.0.8" PrivateAssets="all" />
  </ItemGroup>

</Project>
Program.cs
using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();
app.MapControllers();
app.MapGet("/qwer", () => "Hello!");
app.MapSwagger();
app.UseSwaggerUI();
app.Run();

[ApiController]
public class MyController : ControllerBase
{
    [HttpGet("/asdf")]
    [Produces("text/plain")]
    public string HelloThere() => "Hi!";
}

Start with dotnet run.
Compare obj\a.json (generated by ApiDescription.Server) to https://localhost:5001/swagger/v1/swagger.json (generated by Swagger).
The first does not contain the /qwer endpoint. Diff included for convenience.

diff
 {
   "openapi": "3.0.1",
   "info": {
-    "title": "a, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
+    "title": "a",
     "version": "1.0"
   },
   "paths": {
+    "/qwer": {
+      "get": {
+        "tags": [
+          "a"
+        ],
+        "responses": {
+          "200": {
+            "description": "Success",
+            "content": {
+              "text/plain": {
+                "schema": {
+                  "type": "string"
+                }
+              }
+            }
+          }
+        }
+      }
+    },
    "/asdf": {
      "get": {
        "tags": [
          "My"
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": { }
}

Exceptions (if any)

No exception

.NET Version

6.0.400

Anything else?

dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.400
 Commit:    7771abd614

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19044
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.400\

Host:
  Version:      7.0.0-preview.7.22375.6
  Architecture: x64
  Commit:       eecb028078

.NET SDKs installed:
  6.0.400 [C:\Program Files\dotnet\sdk]
  7.0.100-preview.7.22377.5 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0-preview.7.22376.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0-preview.7.22375.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.0-preview.7.22377.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  arm64 [C:\Program Files\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\arm64\InstallLocation]
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

Metadata

Metadata

Assignees

Labels

bugThis issue describes a behavior which is not expected - a bug.old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions