Skip to content

Commit

Permalink
[Next Major] prevent autopagination from mutating (#2899)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: jar-stripe <[email protected]>
  • Loading branch information
richardm-stripe and jar-stripe authored Feb 5, 2025
1 parent 7a40fcf commit 559e1fe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Stripe.net/Services/_base/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ internal IEnumerable<T> V1ListRequestAutoPagingSync<T>(
options,
requestOptions);

options = options ?? new ListOptions();
options = ((ListOptions)options?.Clone()) ?? new ListOptions();
bool iterateBackward = false;

// Backward iterating activates if we have an `EndingBefore`
Expand Down Expand Up @@ -360,7 +360,7 @@ internal async IAsyncEnumerable<T> V1ListRequestAutoPagingAsync<T>(
requestOptions,
cancellationToken).ConfigureAwait(false);

options = options ?? new ListOptions();
options = ((ListOptions)options?.Clone()) ?? new ListOptions();
bool iterateBackward = false;

// Backward iterating activates if we have an `EndingBefore`
Expand Down Expand Up @@ -450,7 +450,7 @@ private IEnumerable<T> SearchRequestAutoPagingSync<T>(
options,
requestOptions);

options = options ?? new SearchOptions();
options = ((SearchOptions)options?.Clone()) ?? new SearchOptions();

while (true)
{
Expand Down Expand Up @@ -500,7 +500,7 @@ internal async IAsyncEnumerable<T> SearchRequestAutoPagingAsync<T>(
requestOptions,
cancellationToken).ConfigureAwait(false);

options = options ?? new SearchOptions();
options = ((SearchOptions)options?.Clone()) ?? new SearchOptions();

while (true)
{
Expand Down
6 changes: 6 additions & 0 deletions src/StripeTests/Services/AutoPagingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public void ListAutoPaging_StartingAfter()
Assert.Equal("pm_126", models[1].Id);
Assert.Equal("pm_127", models[2].Id);

// Should not mutate its argument
Assert.Equal("pm_124", options.StartingAfter);

// Check invocations
this.MockHttpClientFixture.MockHandler.Protected()
.Verify(
Expand Down Expand Up @@ -247,6 +250,9 @@ public void ListAutoPaging_EndingBefore()
};
var models = service.ListAutoPaging(options).ToList();

// Should not mutate its argument
Assert.Equal("pm_127", options.EndingBefore);

// Check results
Assert.Equal(5, models.Count);
Assert.Equal("pm_126", models[0].Id);
Expand Down
10 changes: 8 additions & 2 deletions src/StripeTests/Services/_base/ServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ public void SearchAuto_ReturnsAllPages()
"?page=page2&query=my+query");

var service = new TestService(this.StripeClient);
var options = new SearchOptions() { Query = "my query" };

HashSet<string> ids = new HashSet<string>();
foreach (var testEntity in service.SearchAutoPaging(new SearchOptions() { Query = "my query" }))
foreach (var testEntity in service.SearchAutoPaging(options))
{
Assert.NotNull(testEntity);
ids.Add(testEntity.Id);
}

Assert.Null(options.Page);

Assert.Equal(4, ids.Count);
Assert.Equal(4, ids.Count);
}
Expand All @@ -91,13 +94,16 @@ public async Task SearchAutoAsync_ReturnsAllPages()

var service = new TestService(this.StripeClient);

var options = new SearchOptions() { Query = "my query" };
HashSet<string> ids = new HashSet<string>();
await foreach (var testEntity in service.SearchAutoPagingAsync(new SearchOptions() { Query = "my query" }))
await foreach (var testEntity in service.SearchAutoPagingAsync(options))
{
Assert.NotNull(testEntity);
ids.Add(testEntity.Id);
}

Assert.Null(options.Page);

Assert.Equal(4, ids.Count);
}

Expand Down

0 comments on commit 559e1fe

Please sign in to comment.