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

Fix duplicate expand values issue #1745

Merged
merged 1 commit into from
Aug 14, 2019
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
11 changes: 10 additions & 1 deletion src/Stripe.net/Services/_base/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,16 @@ protected BaseOptions SetupOptions(BaseOptions options, bool isListMethod)
}

options = options ?? new BaseOptions();
options.AddRangeExpand(serviceExpansions);

if (options.Expand == null)
{
options.Expand = new List<string>();
}

// Compute the union instead of using `AddRangeExpand` to avoid adding duplicate
// values in the list, in case `SetupOptions` has already been called on this options
// instance.
options.Expand = options.Expand.Union(serviceExpansions).ToList();

return options;
}
Expand Down
9 changes: 6 additions & 3 deletions src/StripeTests/Services/AutoPagingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void ListAutoPaging()

// Call auto-paging method
var service = new PageableService(this.StripeClient);
service.ExpandFoo = true;
var options = new ListOptions
{
Limit = 2,
Expand All @@ -66,7 +67,7 @@ public void ListAutoPaging()
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels" &&
m.RequestUri.Query == "?limit=2"),
m.RequestUri.Query == "?limit=2&expand[0]=data.foo"),
ItExpr.IsAny<CancellationToken>());

this.MockHttpClientFixture.MockHandler.Protected()
Expand All @@ -76,7 +77,7 @@ public void ListAutoPaging()
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels" &&
m.RequestUri.Query == "?limit=2&starting_after=pm_124"),
m.RequestUri.Query == "?limit=2&starting_after=pm_124&expand[0]=data.foo"),
ItExpr.IsAny<CancellationToken>());

this.MockHttpClientFixture.MockHandler.Protected()
Expand All @@ -86,7 +87,7 @@ public void ListAutoPaging()
ItExpr.Is<HttpRequestMessage>(m =>
m.Method == HttpMethod.Get &&
m.RequestUri.AbsolutePath == "/v1/pageablemodels" &&
m.RequestUri.Query == "?limit=2&starting_after=pm_126"),
m.RequestUri.Query == "?limit=2&starting_after=pm_126&expand[0]=data.foo"),
ItExpr.IsAny<CancellationToken>());
}

Expand Down Expand Up @@ -170,6 +171,8 @@ public PageableService(IStripeClient client)
{
}

public bool ExpandFoo { get; set; }

public override string BasePath => "/v1/pageablemodels";

public IEnumerable<PageableModel> ListAutoPaging(ListOptions options = null)
Expand Down
13 changes: 12 additions & 1 deletion src/StripeTests/Services/_base/ServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void Get_ExpandProperties()
service.ExpandMultiWordProperty = true;
service.Get("foo");

Assert.Equal(2, client.LastOptions.Expand.Count);
Assert.Contains("simple", client.LastOptions.Expand);
Assert.Contains("multi_word_property", client.LastOptions.Expand);
}
Expand Down Expand Up @@ -60,8 +61,18 @@ public void List_ExpandProperties()

service.ExpandSimple = true;
service.ExpandMultiWordProperty = true;
service.List();
var options = new ListOptions();

service.List(options);

Assert.Equal(2, client.LastOptions.Expand.Count);
Assert.Contains("data.simple", client.LastOptions.Expand);
Assert.Contains("data.multi_word_property", client.LastOptions.Expand);

// Do a second request to check that expand properties are not duplicated
service.List(options);

Assert.Equal(2, client.LastOptions.Expand.Count);
Assert.Contains("data.simple", client.LastOptions.Expand);
Assert.Contains("data.multi_word_property", client.LastOptions.Expand);
}
Expand Down