diff --git a/src/Stripe.net/Services/_base/Service.cs b/src/Stripe.net/Services/_base/Service.cs index 8cfa8294c6..efe4af981b 100644 --- a/src/Stripe.net/Services/_base/Service.cs +++ b/src/Stripe.net/Services/_base/Service.cs @@ -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(); + } + + // 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; } diff --git a/src/StripeTests/Services/AutoPagingTest.cs b/src/StripeTests/Services/AutoPagingTest.cs index bc36d3b3b1..e1c1af05c3 100644 --- a/src/StripeTests/Services/AutoPagingTest.cs +++ b/src/StripeTests/Services/AutoPagingTest.cs @@ -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, @@ -66,7 +67,7 @@ public void ListAutoPaging() ItExpr.Is(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()); this.MockHttpClientFixture.MockHandler.Protected() @@ -76,7 +77,7 @@ public void ListAutoPaging() ItExpr.Is(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()); this.MockHttpClientFixture.MockHandler.Protected() @@ -86,7 +87,7 @@ public void ListAutoPaging() ItExpr.Is(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()); } @@ -170,6 +171,8 @@ public PageableService(IStripeClient client) { } + public bool ExpandFoo { get; set; } + public override string BasePath => "/v1/pageablemodels"; public IEnumerable ListAutoPaging(ListOptions options = null) diff --git a/src/StripeTests/Services/_base/ServiceTest.cs b/src/StripeTests/Services/_base/ServiceTest.cs index da4210457f..4ecf64cb58 100644 --- a/src/StripeTests/Services/_base/ServiceTest.cs +++ b/src/StripeTests/Services/_base/ServiceTest.cs @@ -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); } @@ -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); }