Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal APIs route constraints break OpenAPI requests #36413

Closed
martincostello opened this issue Sep 11, 2021 · 3 comments · Fixed by domaindrivendev/Swashbuckle.AspNetCore#2231
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Priority:1 Work that is critical for the release, but we could probably ship without
Milestone

Comments

@martincostello
Copy link
Member

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

@martincostello
Copy link
Member Author

FYI, I didn't try any of the other possible constraints (like length(n)), but I assume they would have similar issues.

@davidfowl davidfowl added feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Sep 12, 2021
@captainsafia
Copy link
Member

I think this is likely a Swashbuckle issue, similar to the one we had recently with the ? in optional route parameters borking route rendering.

For this one, it looks like it is also happening for route constraints in controller-based APIs as well.

@BrennanConroy
Copy link
Member

Done!

@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
@amcasey amcasey added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels Priority:1 Work that is critical for the release, but we could probably ship without
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants