Skip to content

[Bug]: Minimal API (MapGet) not showing when API published (e.g. to Azure or localhost) #3471

@mattfrear

Description

@mattfrear

Describe the bug

I have 2 endpoints, one a controller and the other a minimal API MapGet endpoint (a health check endpoint).

When I debug the solution locally, both endpoints show on the Swagger page.

Expected behavior

When the solution is published, via dotnet publish and then ran via dotnet mydll.dll, both the controller endpoint and the minimal api should be shown.

Actual behavior

When the solution is published, via dotnet publish and then ran via dotnet mydll.dll, only the controller endpoint is shown, the minimal api is not on the Swagger UI page or in the swagger.json.

Steps to reproduce

  1. Clone https://github.com/mattfrear/Swashbuckle-MapGet-Fail
  2. dotnet publish
  3. dotnet bin/Release/net9.0/publish/WebApplication4.dll
  4. Browse to http://localhost:5000/swagger/ - observe only one endpoint

Here's my Program.cs. I think it's vanilla and installs Swashbuckle as per the instructions.

using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});

var app = builder.Build();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.MapGet("/", () => { return "Always On!"; })
        .WithTags("HealthCheck"); // Azure app service Always On pings this endpoint. So does something else, which pings APIM at the site root "/".
        
app.UseSwagger();

app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("v1/swagger.json", "My API V1");
});
app.Run();

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

I have reproduced this in 6.5.0 and 9.0.1.

.NET Version

9.0.300

Anything else?

Surely I'm not the first one to see this? If I deploy my API to Azure then the Minimal API endpoint isn't in swagger.json.
The minimal API endpoint is however present and can be called.
I'm pretty sure the Minimal API was visible about a month ago, as I had previously hidden it deliberately with .ExcludeFromDescription(). However, now I want it to be in the swagger as I want to import my health check into Azure API Management. So I wonder if this issue is caused by .my specific version of NET 9.

local debug:

Image

dotnet publish:
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstaleStale issues or pull requests

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions