The Wayback Machine - https://web.archive.org/web/20220113084122/https://github.com/dotnet/aspnetcore/issues/39486
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

Analyzer: warn when marking a route parameter as optional if it isn't at the end of a route #39486

Open
Jcparkyn opened this issue Jan 13, 2022 · 0 comments

Comments

@Jcparkyn
Copy link

@Jcparkyn Jcparkyn commented Jan 13, 2022

Is your feature request related to a problem? Please describe.

Route parameters in an ASP.NET Core Web API can be marked as optional with a ?, e.g.,

app.MapGet("/test1/{foo?}", (string? foo) => foo ?? "none"); // "/test1" returns "none"

If a route parameter marked as optional is followed by a non-optional route segment, the optional annotation is ignored. E.g.,

app.MapGet("/test2/{foo?}/bar", (string? foo) => foo ?? "none"); // "/test2//bar" returns 404

It is not immediately obvious (to me at least) that it would not be possible to have an optional parameter within a route, and there is no run-time error or warning when doing this.

The behavior appears to be the same for nullable value types (e.g. int?).

Describe the solution you'd like

An analyzer that checks for routes containing optional parameters followed by non-optional route segments, and provides an appropriate warning.

I also couldn't find any mention in the documentation stating that this is not possible.

Additional context

Optional route parameters are allowed to be followed by other optional parameters, so the analyzer should consider this case. E.g.,

app.MapGet("/test3/{foo?}/{bar?}", (string? foo, string? bar) => foo ?? bar ?? "none"); // "/test3" returns "none"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant