Use ValueTuple element names in minimal route handlers
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
Take the following minimal route handler:
app.MapPut("/todos/{id}", async ((int Id, string Title, bool IsComplete) inputTodo, TodoDb db) =>
Today, to upload an inputTodoo to this endpoint, you would need a JSON request body like the following because we don’t look for any TupleElementNameAttributes on the parameter:
{
"Item1": 1,
"Item2": "Support named items in ValueTuples",
"Item3": false
}
Describe the solution you’d like
If we took the names of the ValueTuple elements into account, the request body could look like you’d expect:
{
"Id": 1,
"Title": "Support named items in ValueTuples",
"IsComplete": false
}
Additional context
We have the same problem with ValueTuples returned from minimal route handlers. Today, the following would be serialized using Item1
, Item2
and Item3
as the property values instead of the specified names.
app.MapGet("/todos/{id}", async (int Id, string Title, bool IsComplete) (int id, TodoDb db) =>
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:8 (6 by maintainers)
Top Results From Across the Web
C# ValueTuple properties naming
ValueTuple is just a normal C# type with no special support from the compiler. · "but, like this: ValueTuple<string,int> tuple2 = (Name: "Name1", ......
Read more >Route handlers in Minimal API apps
Route handlers are methods that execute when the route matches. ... Using a named endpoint avoids having to hard code paths in an...
Read more >Minimal API growing with .NET 7 - csharp.christiannagel.com
The minimal API makes use of top-level statements and is based on some C# language enhancements such as lambda expression improvements such as ......
Read more >C# ValueTuple
It is easy to create and initialize the ValueTuple . It can be created and initialized using parentheses () and specifying the values...
Read more >All about ValueTuple field names in C# - Duong's Blog
Starting from C# 7.0, we can use the System.ValueTuple type instead. As its name suggests, it is a value type. And it supports...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @davidfowl. That’s similar to what I am doing atm,
Although I am kinda hoping I don’t have to create another request dto. Cheers
I don’t see how this might be possible to solve in the general case, for the reasons @davidfowl already mentioned. I’m also not sure how a custom
JsonConverter
might be able to work with ambiguity such as((int foo, int bar), (int baz, int qux))
. As far as the STJ converter infrastructure is concerned, both nested values are of typeValueTuple<int, int>
and as such they will be dispatched to the same converter instance for serialization without any contextual information.It might be worth pointing out that the source generator can in fact retrieve tuple label metadata from Roslyn, however such an approach would be problematic for a couple of reasons:
Type
, and as such it would not be possible to address ambiguities like the one highlighted above.