Skip to content

Minimal APIs route constraints break OpenAPI requests #36413

@martincostello

Description

@martincostello

Describe the bug

If a Minimal API endpoint uses a route constraint, for example "/status/{code:range(200, 599)}", the constraint is exposed to OpenAPI documentation incorrectly, which causes the API to be called incorrectly, resulting in an HTTP 404.

https://localhost:7168/status/%7Bcode:range(200,%20599)%7D

image

To Reproduce

Create an empty ASP.NET Core 6 project with a reference to Swashbuckle.AspNetCore with the below code as Program.cs.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new() { Title = builder.Environment.ApplicationName, Version = "v1" });
});

var app = builder.Build();

app.MapGet("/status/{code:range(200, 599)}", (int code) => Results.StatusCode(code));

app.UseSwagger();
app.UseSwaggerUI(options =>
{
    options.EnableTryItOutByDefault();
    options.SwaggerEndpoint("/swagger/v1/swagger.json", $"{builder.Environment.ApplicationName} v1");
});

app.Run();

Further technical details

.NET SDK version 6.0.100-rc.1.21460.8

Metadata

Metadata

Assignees

Labels

Priority:1Work that is critical for the release, but we could probably ship withoutarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions