Repository: mailgun/mailgun-go (go)
Issue: ListMembers: vars duplicated on subsequent pages
There is an issue with the mailing list `ListMembers` method where vars from a previous page will be copied to the next page if vars aren't set on that page.

See this example:

```go
func main() {
	ctx := context.Background()
	listAddr := "test@mydomain.com"

	client := mailgun.NewMailgun("mydomain.com", "my_key")

	for i := 0; i < 200; i++ {
		memberAddr := fmt.Sprintf("%03d@example.com", i)

		var vars map[string]interface{}
		if i == 5 {
			vars = map[string]interface{}{"has": "vars"}
		}

		err := client.CreateMember(ctx, false, listAddr, mailgun.Member{
			Address: memberAddr,
			Vars:    vars,
		})
		if err != nil {
			panic(err)
		}
	}

	it := client.ListMembers(listAddr, nil)

	var members []mailgun.Member
	for it.Next(ctx, &members) {
		for _, m := range members {
			fmt.Println(m.Address, m.Vars)
		}
	}
	if err := it.Err(); err != nil {
		panic(err)
	}
}
```

You would expect this code to only return vars for `005@example.com`, but it also returns vars for `105@example.com` because it's in the same position on the next page returned by the iterator.

Output here:

```
000@example.com map[]
001@example.com map[]
002@example.com map[]
003@example.com map[]
004@example.com map[]
005@example.com map[has:vars]
006@example.com map[]
007@example.com map[]
008@example.com map[]
009@example.com map[]
010@example.com map[]
011@example.com map[]
012@example.com map[]
013@example.com map[]
014@example.com map[]
015@example.com map[]
016@example.com map[]
017@example.com map[]
018@example.com map[]
019@example.com map[]
020@example.com map[]
021@example.com map[]
022@example.com map[]
023@example.com map[]
024@example.com map[]
025@example.com map[]
026@example.com map[]
027@example.com map[]
028@example.com map[]
029@example.com map[]
030@example.com map[]
031@example.com map[]
032@example.com map[]
033@example.com map[]
034@example.com map[]
035@example.com map[]
036@example.com map[]
037@example.com map[]
038@example.com map[]
039@example.com map[]
040@example.com map[]
041@example.com map[]
042@example.com map[]
043@example.com map[]
044@example.com map[]
045@example.com map[]
046@example.com map[]
047@example.com map[]
048@example.com map[]
049@example.com map[]
050@example.com map[]
051@example.com map[]
052@example.com map[]
053@example.com map[]
054@example.com map[]
055@example.com map[]
056@example.com map[]
057@example.com map[]
058@example.com map[]
059@example.com map[]
060@example.com map[]
061@example.com map[]
062@example.com map[]
063@example.com map[]
064@example.com map[]
065@example.com map[]
066@example.com map[]
067@example.com map[]
068@example.com map[]
069@example.com map[]
070@example.com map[]
071@example.com map[]
072@example.com map[]
073@example.com map[]
074@example.com map[]
075@example.com map[]
076@example.com map[]
077@example.com map[]
078@example.com map[]
079@example.com map[]
080@example.com map[]
081@example.com map[]
082@example.com map[]
083@example.com map[]
084@example.com map[]
085@example.com map[]
086@example.com map[]
087@example.com map[]
088@example.com map[]
089@example.com map[]
090@example.com map[]
091@example.com map[]
092@example.com map[]
093@example.com map[]
094@example.com map[]
095@example.com map[]
096@example.com map[]
097@example.com map[]
098@example.com map[]
099@example.com map[]
100@example.com map[]
101@example.com map[]
102@example.com map[]
103@example.com map[]
104@example.com map[]
105@example.com map[has:vars]
106@example.com map[]
107@example.com map[]
108@example.com map[]
109@example.com map[]
110@example.com map[]
111@example.com map[]
112@example.com map[]
113@example.com map[]
114@example.com map[]
115@example.com map[]
116@example.com map[]
117@example.com map[]
118@example.com map[]
119@example.com map[]
120@example.com map[]
121@example.com map[]
122@example.com map[]
123@example.com map[]
124@example.com map[]
125@example.com map[]
126@example.com map[]
127@example.com map[]
128@example.com map[]
129@example.com map[]
130@example.com map[]
131@example.com map[]
132@example.com map[]
133@example.com map[]
134@example.com map[]
135@example.com map[]
136@example.com map[]
137@example.com map[]
138@example.com map[]
139@example.com map[]
140@example.com map[]
141@example.com map[]
142@example.com map[]
143@example.com map[]
144@example.com map[]
145@example.com map[]
146@example.com map[]
147@example.com map[]
148@example.com map[]
149@example.com map[]
150@example.com map[]
151@example.com map[]
152@example.com map[]
153@example.com map[]
154@example.com map[]
155@example.com map[]
156@example.com map[]
157@example.com map[]
158@example.com map[]
159@example.com map[]
160@example.com map[]
161@example.com map[]
162@example.com map[]
163@example.com map[]
164@example.com map[]
165@example.com map[]
166@example.com map[]
167@example.com map[]
168@example.com map[]
169@example.com map[]
170@example.com map[]
171@example.com map[]
172@example.com map[]
173@example.com map[]
174@example.com map[]
175@example.com map[]
176@example.com map[]
177@example.com map[]
178@example.com map[]
179@example.com map[]
180@example.com map[]
181@example.com map[]
182@example.com map[]
183@example.com map[]
184@example.com map[]
185@example.com map[]
186@example.com map[]
187@example.com map[]
188@example.com map[]
189@example.com map[]
190@example.com map[]
191@example.com map[]
192@example.com map[]
193@example.com map[]
194@example.com map[]
195@example.com map[]
196@example.com map[]
197@example.com map[]
198@example.com map[]
199@example.com map[]
```

Pull request description: Clear list before unmarshalling responses during paging
Fixes #247 
This also bumps the required version to 1.13.15 for #245 

Patch:
diff --git a/.travis.yml b/.travis.yml
index 043e476..e67f7b6 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,4 +4,4 @@ env:
   - GO111MODULE=on
 
 go:
-  - 1.11.x
+  - 1.13.15
diff --git a/bounces.go b/bounces.go
index 0599c8e..89ef094 100644
--- a/bounces.go
+++ b/bounces.go
@@ -141,6 +141,7 @@ func (ci *BouncesIterator) Previous(ctx context.Context, items *[]Bounce) bool {
 }
 
 func (ci *BouncesIterator) fetch(ctx context.Context, url string) error {
+	ci.Items = nil
 	r := newHTTPRequest(url)
 	r.setClient(ci.mg.Client())
 	r.setBasicAuth(basicAuthUser, ci.mg.APIKey())
diff --git a/credentials.go b/credentials.go
index b13fe18..91d0b04 100644
--- a/credentials.go
+++ b/credentials.go
@@ -160,6 +160,7 @@ func (ri *CredentialsIterator) Previous(ctx context.Context, items *[]Credential
 }
 
 func (ri *CredentialsIterator) fetch(ctx context.Context, skip, limit int) error {
+	ri.Items = nil
 	r := newHTTPRequest(ri.url)
 	r.setBasicAuth(basicAuthUser, ri.mg.APIKey())
 	r.setClient(ri.mg.Client())
diff --git a/domains.go b/domains.go
index 4edf75e..a1b3b71 100644
--- a/domains.go
+++ b/domains.go
@@ -217,6 +217,7 @@ func (ri *DomainsIterator) Previous(ctx context.Context, items *[]Domain) bool {
 }
 
 func (ri *DomainsIterator) fetch(ctx context.Context, skip, limit int) error {
+	ri.Items = nil
 	r := newHTTPRequest(ri.url)
 	r.setBasicAuth(basicAuthUser, ri.mg.APIKey())
 	r.setClient(ri.mg.Client())
diff --git a/events.go b/events.go
index 97e5c33..8e7a768 100644
--- a/events.go
+++ b/events.go
@@ -160,6 +160,7 @@ func (ei *EventIterator) Previous(ctx context.Context, events *[]Event) bool {
 }
 
 func (ei *EventIterator) fetch(ctx context.Context, url string) error {
+	ei.Items = nil
 	r := newHTTPRequest(url)
 	r.setClient(ei.mg.Client())
 	r.setBasicAuth(basicAuthUser, ei.mg.APIKey())
diff --git a/mailing_lists.go b/mailing_lists.go
index 4c1bcde..e29e100 100644
--- a/mailing_lists.go
+++ b/mailing_lists.go
@@ -151,6 +151,7 @@ func (li *ListsIterator) Previous(ctx context.Context, items *[]MailingList) boo
 }
 
 func (li *ListsIterator) fetch(ctx context.Context, url string) error {
+	li.Items = nil
 	r := newHTTPRequest(url)
 	r.setClient(li.mg.Client())
 	r.setBasicAuth(basicAuthUser, li.mg.APIKey())
diff --git a/members.go b/members.go
index ddf801a..1a46c2e 100644
--- a/members.go
+++ b/members.go
@@ -148,6 +148,7 @@ func (li *MemberListIterator) Previous(ctx context.Context, items *[]Member) boo
 }
 
 func (li *MemberListIterator) fetch(ctx context.Context, url string) error {
+	li.Lists = nil
 	r := newHTTPRequest(url)
 	r.setClient(li.mg.Client())
 	r.setBasicAuth(basicAuthUser, li.mg.APIKey())
diff --git a/routes.go b/routes.go
index 61cbca3..3aec677 100644
--- a/routes.go
+++ b/routes.go
@@ -178,6 +178,7 @@ func (ri *RoutesIterator) Previous(ctx context.Context, items *[]Route) bool {
 }
 
 func (ri *RoutesIterator) fetch(ctx context.Context, skip, limit int) error {
+	ri.Items = nil
 	r := newHTTPRequest(ri.url)
 	r.setBasicAuth(basicAuthUser, ri.mg.APIKey())
 	r.setClient(ri.mg.Client())
diff --git a/spam_complaints.go b/spam_complaints.go
index 549b5c7..3f3f2ff 100644
--- a/spam_complaints.go
+++ b/spam_complaints.go
@@ -132,6 +132,7 @@ func (ci *ComplaintsIterator) Previous(ctx context.Context, items *[]Complaint)
 }
 
 func (ci *ComplaintsIterator) fetch(ctx context.Context, url string) error {
+	ci.Items = nil
 	r := newHTTPRequest(url)
 	r.setClient(ci.mg.Client())
 	r.setBasicAuth(basicAuthUser, ci.mg.APIKey())
diff --git a/tags.go b/tags.go
index 682cb19..ed32602 100644
--- a/tags.go
+++ b/tags.go
@@ -158,6 +158,7 @@ func (ti *TagIterator) Err() error {
 }
 
 func (ti *TagIterator) fetch(ctx context.Context, url string) error {
+	ti.Items = nil
 	req := newHTTPRequest(url)
 	req.setClient(ti.mg.Client())
 	req.setBasicAuth(basicAuthUser, ti.mg.APIKey())
diff --git a/template.go b/template.go
index 7e7ba95..00d33e6 100644
--- a/template.go
+++ b/template.go
@@ -234,6 +234,7 @@ func (ti *TemplatesIterator) Previous(ctx context.Context, items *[]Template) bo
 }
 
 func (ti *TemplatesIterator) fetch(ctx context.Context, url string) error {
+	ti.Items = nil
 	r := newHTTPRequest(url)
 	r.setClient(ti.mg.Client())
 	r.setBasicAuth(basicAuthUser, ti.mg.APIKey())
diff --git a/template_versions.go b/template_versions.go
index 418e4b6..97f7317 100644
--- a/template_versions.go
+++ b/template_versions.go
@@ -209,6 +209,7 @@ func (li *TemplateVersionsIterator) Previous(ctx context.Context, items *[]Templ
 }
 
 func (li *TemplateVersionsIterator) fetch(ctx context.Context, url string) error {
+	li.Template.Versions = nil
 	r := newHTTPRequest(url)
 	r.setClient(li.mg.Client())
 	r.setBasicAuth(basicAuthUser, li.mg.APIKey())
diff --git a/unsubscribes.go b/unsubscribes.go
index 9059b33..fd81a07 100644
--- a/unsubscribes.go
+++ b/unsubscribes.go
@@ -125,6 +125,7 @@ func (ci *UnsubscribesIterator) Previous(ctx context.Context, items *[]Unsubscri
 }
 
 func (ci *UnsubscribesIterator) fetch(ctx context.Context, url string) error {
+	ci.Items = nil
 	r := newHTTPRequest(url)
 	r.setClient(ci.mg.Client())
 	r.setBasicAuth(basicAuthUser, ci.mg.APIKey())


Test patch:
diff --git a/mailing_lists_test.go b/mailing_lists_test.go
index e1bddb7..81da250 100644
--- a/mailing_lists_test.go
+++ b/mailing_lists_test.go
@@ -2,6 +2,7 @@ package mailgun_test
 
 import (
 	"context"
+	"fmt"
 	"testing"
 
 	"github.com/facebookgo/ensure"
@@ -146,3 +147,45 @@ func TestMailingLists(t *testing.T) {
 	newList.Description = "A list whose description changed"
 	ensure.DeepEqual(t, theList, newList)
 }
+
+func TestListMailingListRegression(t *testing.T) {
+	mg := mailgun.NewMailgun(testDomain, testKey)
+	mg.SetAPIBase(server.URL())
+	ctx := context.Background()
+	address := "test@example.com"
+
+	_, err := mg.CreateMailingList(ctx, mailgun.MailingList{
+		Address:     address,
+		Name:        "paging",
+		Description: "Test paging",
+	})
+	ensure.Nil(t, err)
+
+	for i := 0; i < 200; i++ {
+		var vars map[string]interface{}
+		if i == 5 {
+			vars = map[string]interface{}{"has": "vars"}
+		}
+
+		err := mg.CreateMember(ctx, false, address, mailgun.Member{
+			Address: fmt.Sprintf("%03d@example.com", i),
+			Vars:    vars,
+		})
+		ensure.Nil(t, err)
+	}
+
+	it := mg.ListMembers(address, nil)
+
+	var members []mailgun.Member
+	var found int
+	for it.Next(ctx, &members) {
+		for _, m := range members {
+			if m.Vars != nil {
+				found++
+			}
+			//t.Logf("%v %v", m.Address, m.Vars)
+		}
+	}
+	ensure.DeepEqual(t, found, 1)
+	ensure.Nil(t, err)
+}