-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Catch-all route parameter should be marked as optional in ApiExplorer #60392
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
Comments
Thanks for filing this issue!
I agree with this sentiment! It seems like you've done a bit og digging int othe space. Would you be interested in submitting a PR? You'll want to modify the IApiDescriptionProvider interfaces in the repo to react to this change. We already do the due dilligence in the OpenAPI package to make sure that path parameters are always required so there shouldn't be a need to harden anything there. |
Hi @captainsafia, @martincostello, @mikekistler and @scharnyw , I'd like to work on this issue! I've read the description and understand the problem. I'm also grateful for the guidance provided on where to start looking in the codebase. Would it be possible to assign this issue to me? I'm excited to contribute! |
- Added a unit test in DefaultApiDescriptionProviderTest to validate that catch-all route parameters (e.g., "{**catchAllParameter}") are reported as optional. - Modified ProcessIsRequired in DefaultApiDescriptionProvider to detect catch-all parameters (by checking for the "{**" pattern in the route template) and mark them as not required. - This change aligns ApiExplorer metadata with the actual runtime behavior, ensuring more accurate API documentation. - Follows TDD practices by first writing a failing test and then implementing the fix.
I have submitted a PR for this issue: #60544 |
…ers as optional Previously, ApiExplorer marked all parameters as optional if the route template contained a catch-all marker ("{**"). This update refines the logic by leveraging ApiParameterContext.RouteParameters and the IsCatchAll flag to identify and mark only the catch-all parameter as optional. This change aligns the API metadata with the actual routing semantics and addresses dotnet#60392.
* Fix: Mark catch-all route parameters as optional (#60392) - Added a unit test in DefaultApiDescriptionProviderTest to validate that catch-all route parameters (e.g., "{**catchAllParameter}") are reported as optional. - Modified ProcessIsRequired in DefaultApiDescriptionProvider to detect catch-all parameters (by checking for the "{**" pattern in the route template) and mark them as not required. - This change aligns ApiExplorer metadata with the actual runtime behavior, ensuring more accurate API documentation. - Follows TDD practices by first writing a failing test and then implementing the fix. * Fix: Use route parameter metadata to correctly mark catch-all parameters as optional Previously, ApiExplorer marked all parameters as optional if the route template contained a catch-all marker ("{**"). This update refines the logic by leveraging ApiParameterContext.RouteParameters and the IsCatchAll flag to identify and mark only the catch-all parameter as optional. This change aligns the API metadata with the actual routing semantics and addresses #60392.
Is there an existing issue for this?
Describe the bug
Catch-all route parameter is effectively optional because it matches against all values, including whitespace and empty strings. For example, consider the following API:
This API can be reached in any of the following ways:
This behavior of catch-all parameter is consistent with the documentation. It is also consistent between Controllers and Minimal API, except that there is currently a regression bug (#58093) in Minimal API in .NET 8 that prevented it from working (it worked fine in .NET 7).
However, in the above code snippet
parameter.IsRequired
always reportstrue
. This is effectively a lie because the API can be reached without the parameter being set.While I understand that ApiExplorer is frequently used for OpenAPI generation purposes, and that the OpenAPI specification does not allow for optional path parameters, I think the ASP.NET Core ApiExplorer's behavior should not be driven solely by OpenAPI. Instead, it should faithfully report the actual API's behavior and let some other component be concerned with OpenAPI compatibility when generating the OpenAPI docs.
Expected Behavior
ApiParameterDescription.IsRequired
should reportfalse
for catch-all route parameters.Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
8.0.10
Anything else?
No response
The text was updated successfully, but these errors were encountered: