-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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 API] Route parameters are not set as required when using WithOpenApi and Nullable Reference Types are disabled #46746
Comments
I think the problem could be this statement: |
This is expected behavior based on the way optionality rules work in minimal APIs (in both the RequestDelegateFactory and the OpenAPI generator). If nullability is disabled in an application context, then we always assume that a parameter is optional (because it's not possible to guarantee that it is required). If nullability is enabled, we treat a parameter as optional if it has a default value or if it has been annotated with the nullability modifier. Your observation that this behavior doesn't make sense for route parameters that are part of the routing path is a sensible one, and highlights the distinction in behavior between what happens at the routing layer (the
The behavior here is correct because it matches the logic that the RequestDelegateFactory uses to construct the parameter binding logic, see https://github.com/dotnet/dotnet/blob/8d5c52c5dd54417a2071c196663bb29ab1ee2442/src/aspnetcore/src/Http/Http.Extensions/src/RequestDelegateFactory.cs#L2020-L2036. Marking as investigate to see if it is possible to make the generator smarter about parameters needed in the route but not the handler. |
To learn more about what this message means, what to expect next, and how this issue will be handled you can read our Triage Process document. |
So I think this behavior should be aligned in the Swagger UI as well, otherwise we have the issue I mentioned before, do you agree? |
@marcominerva Yes, you're correct. I'd like to retract the explainer that I provided in #46746 (comment). The behavior I mentioned is accurate for non-route parameters. OpenAPI requires that all route parameters be marked as required, which is incongruent with the concept of optional route parameters that we have in ASP.NET Core. I believe the correct thing to do here is to treat all route parameters as required. I'll fix this in the |
The built-in OpenAPI support now follows the correct behavior per the OpenAPI spec and treats all parameters coming from the path as required regardless of their nullability status. I've added a test case in #55321 to cover this. In the future, I plan on replating the Closing this is as resolved for now. |
Is there an existing issue for this?
Describe the bug
If I define this endpoint:
swagger.json file correctly sets the
group
route parameter as required:However, If I disable Nullable Reference Types, than this parameter is no longer marked as required:
If I try to invoke the endpoint using Swagger UI, a comma is passed as route argument:
The problem is related to the
WithOpenApi
extension method. In fact, if I remove it, than thegroup
route parameter is again set as required:Expected Behavior
The
group
route parameter should be required in any cases, regardless the presence of Nullable Reference Types and the usage ofWithOpenApi
.Steps To Reproduce
Minimal repro here: https://github.com/marcominerva/PathIssue
Exceptions (if any)
No response
.NET Version
7.0.103
Anything else?
No response
The text was updated successfully, but these errors were encountered: