-
-
Notifications
You must be signed in to change notification settings - Fork 746
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
[Bug]: "CancellationToken?" mishandling in refit requests #1915
Comments
Hey, thanks for the bug report. I'm shocked this wasn't discovered earlier 😄. Thankfully, I don't think many people are using nullable cancellation tokens as they are a |
Thank you for your quick answer ! If it's not possible to use the nullable cancellation token, at least throwing an exception as you have suggested rather than serializing it in query would avoid to end up with our situation :). |
I'm leaning towards throwing an exception and raising a diagnostic. |
I guess if somebody pass a nullable cancellation token with a null value, it's because he wants a cancellation token for his query, not some bits of cancellation token in its url :D. So I don't think anybody would be surprised if the "null" CT was suddenly properly handled as a CancellationToken.None, neither if the nullable argument itself was correctly processed :p. Actually, regarding existing usages of the library, an exception coming from nowhere in future releases is an higher risk, as before it was just an "almost silent error" that nobody has never noticed. Thank you again :) |
Hello, |
We need to do a few tests on this, I would certainly agree with |
Describe the bug 🐞
Hello,
We are happily using Refit in a project with a lot of calls like :
(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:
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
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.
The text was updated successfully, but these errors were encountered: