Description
Describe the bug 🐞
Hello,
We are happily using Refit in a project with a lot of calls like :
[Post("SomeRoute)]
public Task<ApiResponse<SomeDto>> DoSomething(CancellationToken? cancellationToken = null);
(this example is for POST, but PUT or GET does the same)
We have noticed that when a CancellationToken is provided, it is handled as a query string parameter :
(note the IsCancellationRequested, CanBeCancelled... Appearing in the query string).
If the signature is updated as below, the CancellationToken is handled as expected:
[Post("SomeRoute)]
public Task<ApiResponse<SomeDto>> DoSomething(CancellationToken cancellationToken);
But we were willing to make the CancellationToken optional, hence the "?" and "= null". We didn't figure it won't work.
One workaround would be to change the signature for
[Post("SomeRoute)]
public Task<ApiResponse<SomeDto>> DoSomething(CancellationToken cancellationToken = default);
to make the parameter both non nullable and optional, "default" does exactly what a CancellationToken.None does:
Do you have a suggestion regarding my workaround above, is it good for you ? Do you plan to improve this to handle "nullable CancellationToken" ?
Thank you very much !
Step to reproduce
Write a refit method like
[Post("SomeRoute)]
public Task<ApiResponse> DoSomething(CancellationToken? cancellationToken = null);
and see query string.
Expected behavior
It would be nice and safer if nullable "CancellationToken?" methods signatures could be handled as non nullable "CancellationToken" instead of to be processed as query string.
IDE
Visual Studio 2022
Operating system
Windows
Refit Version
Refit.Newtonsoft.Json (and refit) in version 7.2.1 with .Net 8. Same behavior with Refit.Newtonsoft.Json in version 8.
Activity