Skip to content
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

feat: prevent serialization of CancellationToken? #1917

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ public interface IFragmentApi
Task QueryAfterFragment();
}

public interface ICancellableApi
{
[Get("/foo")]
Task GetWithCancellation(CancellationToken token = default);

[Get("/foo")]
Task<string> GetWithCancellationAndReturn(CancellationToken token = default);

[Get("/foo")]
Task GetWithNullableCancellation(CancellationToken? token);
}

public class HttpBinGet
{
public Dictionary<string, object> Args { get; set; }
Expand Down Expand Up @@ -2547,6 +2559,50 @@ public async Task ShouldStripQueryAfterFragment()
mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task TaskShouldCancelWhenRequested()
{
var ctSource = new CancellationTokenSource();
var token = ctSource.Token;

var fixture = RestService.For<ICancellableApi>("https://github.com");

ctSource.Cancel();
var task = fixture.GetWithCancellation(token);
await Assert.ThrowsAsync<TaskCanceledException>(async () => await task);
}

[Fact]
public async Task TaskResultShouldCancelWhenRequested()
{
var ctSource = new CancellationTokenSource();
var token = ctSource.Token;

var fixture = RestService.For<ICancellableApi>("https://github.com");

ctSource.Cancel();
var task = fixture.GetWithCancellationAndReturn(token);
await Assert.ThrowsAsync<TaskCanceledException>(async () => await task);
}


[Fact]
public async Task NullableCancellationTokenShouldBeIgnored()
{
var mockHttp = new MockHttpMessageHandler();
var settings = new RefitSettings { HttpMessageHandlerFactory = () => mockHttp, };

mockHttp
.Expect(HttpMethod.Get, "https://github.com/foo")
.Respond(HttpStatusCode.OK);

var fixture = RestService.For<ICancellableApi>("https://github.com", settings);

await fixture.GetWithNullableCancellation(null);

mockHttp.VerifyNoOutstandingExpectation();
}

[Fact]
public async Task TypeCollisionTest()
{
Expand Down
2 changes: 1 addition & 1 deletion Refit/RestMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public RestMethodInfoInternal(
// Exclude cancellation token parameters from this list
ParameterInfoArray = methodInfo
.GetParameters()
.Where(static p => p.ParameterType != typeof(CancellationToken))
.Where(static p => p.ParameterType != typeof(CancellationToken) && p.ParameterType != typeof(CancellationToken?))
.ToArray();
(ParameterMap, FragmentPath) = BuildParameterMap(RelativePath, ParameterInfoArray);
BodyParameterInfo = FindBodyParameter(ParameterInfoArray, IsMultipart, hma.Method);
Expand Down
Loading