-
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
Use nullibility to infer optionality of service and body parameters #32375
Comments
Thanks for contacting us. We're moving this issue to the |
Also detect default values: app.MapPost("/todos", (Todo todo = null) =>
{
}); Essentially signals from the type system that the user is willing to tolerate null. |
Required reflection metadata about whether parameters have default values and are nullable is not available on private and compiler-generated methods. Also semi-blocked on dotnet/runtime#29723 (having this API is nice, but we can do this ourselves if API is not available) |
We want to determine if these parameters should fail to bind if there's no value provided for them from the determined source (inferred or explicit). The question is, how does the developer indicate that a parameter is optional (should allow default value) or not. For example string Hello(string name = "John") => $"Hello {name}";
app.MapGet("/hi", Hello); In the above, if no name is provided (via the query string), the value will be "John". string Hello(string name = null) => $"Hello {name}";
app.MapGet("/hi", Hello); In the above, if no name is provided, the value will be null. string Hello(string name) => $"Hello {name}";
app.MapGet("/hi", Hello); What does the above mean? Should it mean non-null? Today if no name is provided the value will be null. We need a way to opt-into "required-ness":
// Required name
string Hello(string name) => $"Hello {name}";
// Required name
string Hello([Required]string name) => $"Hello {name}";
// Optional name
string Hello(string name = null) => $"Hello {name}";
// Optional name
string Hello(string? name) => $"Hello {name}"; The same would apply for body parameters: // Required TodoItem
async Task<IResult> PostTodo(TodoItem todo)
{
await db.Todos.AddAsync(todo);
await db.SaveChangesAsync();
return Created($"/todo/{todo.Id}", todo);
}
// Required TodoItem
async Task<IResult> PostTodo([Required]TodoItem todo)
{
await db.Todos.AddAsync(todo);
await db.SaveChangesAsync();
return Created($"/todo/{todo.Id}", todo);
}
// Optional TodoItem (can be null)
async Task<IResult> PostTodo(TodoItem todo = null)
{
if (todo is null)
{
return UnprocessableEntity();
}
await db.Todos.AddAsync(todo);
await db.SaveChangesAsync();
return Created($"/todo/{todo.Id}", todo);
}
// Optional TodoItem (can be null)
async Task<IResult> PostTodo(TodoItem? todo)
{
if (todo is null)
{
return UnprocessableEntity();
}
await db.Todos.AddAsync(todo);
await db.SaveChangesAsync();
return Created($"/todo/{todo.Id}", todo);
} |
We've decided to avoid |
In cases like the above, we could use the nullability of the
Todo
andIService
parameters to determine weather to request body and service are optional.See #31658 (comment)
The text was updated successfully, but these errors were encountered: