question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use ValueTuple element names in minimal route handlers

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
zyofengcommented, Dec 18, 2022

Thanks @davidfowl. That’s similar to what I am doing atm,

        group.MapPost("{accountNumber}/transactions",
                (ISender sender, string accountNumber, GetTransactionsRequest body) => sender.Send(
                    new GetTransactionsQuery
                    {
                        AccountNumber = accountNumber,
                        PageSize = body.PageSize,
                        From = body.From,
                        To = body.To,
                        CurrentPage = body.CurrentPage
                    }))
            .WithName("Transactions");

        return app;
    }
    public record GetTransactionsRequest(DateOnly From, DateOnly To, int CurrentPage, int PageSize = 20);

Although I am kinda hoping I don’t have to create another request dto. Cheers

1reaction
eiriktsarpaliscommented, Mar 3, 2022

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 type ValueTuple<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:

  1. Contract divergence from the reflection-based serializer.
  2. Converters either source generated or otherwise are still indexed by Type, and as such it would not be possible to address ambiguities like the one highlighted above.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found