-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Honor default value provided in parameters for route handlers #47266
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
params
arguments in RDG
Thanks for bringing this up, it is something I hadn't considered. Just looking into the errors in the generated code now for these scenarios. |
OK for the |
In the case of this delegate signature: static string HandleRequest(int purple, [System.Runtime.InteropServices.OptionalAttribute] int orange) => "Hello World!"; Omitting the The remaining case ( |
@amcasey had brought up that it might make sense for
Yep, don't think this is a scenario we'd support. If we did, it would be via the DataAnnotations attributes and the minimal validation support.
Yeah, it looks like this is a result of the fact that code generated by RDF is oblivious to nullability while RDG code is not. I think we should create a separate issue for this to link in the doc. I dug into this a little bit further and it looks like for the given code: string HandleRequest(int purple, int orange = 1) => "Hello World!";
app.MapGet("/hello", HandleRequest); We end up generating a parsing block that looks like this: if (GeneratedRouteBuilderExtensionsCore.TryParseExplicit<int>(orange_temp!, CultureInfo.InvariantCulture, out var orange_temp_parsed_non_nullable))
{
orange_parsed_temp = orange_temp_parsed_non_nullable;
}
else if (string.IsNullOrEmpty(orange_temp))
{
orange_parsed_temp = null;
}
else
{
wasParamCheckFailure = true;
} With the problematic code in question being:
aspnetcore/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/EndpointParameter.cs Line 232 in 7b61996
using a strategy similar to what we do for the delegate cast: aspnetcore/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs Lines 36 to 37 in 7b61996
Edit: Correcting myself. We don't need to use an inline conditional like we do in the delegate cast code. We're already in the aspnetcore/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/EndpointParameter.cs Line 228 in 7b61996
So we should always emit |
params
arguments in RDGTryParsable
-types with default value
TryParsable
-types with default value
I ended up addressing this in #47657 since I imported some tests from RDF that were failing because we didn't handle default values. |
Thanks @captainsafia |
I'll keep this open until the PR is merged... |
@captainsafia - can this be closed now? It is in the "Done" column for both projects, but this issue is still open. |
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
There are a number of ways to make it unnecessary to pass an argument corresponding to a given parameter. We should think about what we want to do in these scenarios with RDG.
Describe the solution you'd like
I see that parameters can't already be flagged as optional:
Maybe we just need to recognize more forms of optionality?
Additional context
No response
The text was updated successfully, but these errors were encountered: