Skip to content

[Bug]: "CancellationToken?" mishandling in refit requests #1915

Open
@AFract

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 :
image
(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:
image

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions