Commit e8780f8
Konstantinos Bairaktaris
(jsonapi) Fix mutability issue with collections
Here is the problem: Lets assume you have a paginated collection:
```python
organization = transifex_api.Organization.get(...)
first_page = organization.fetch("projects")
first_page._params # <<< {}
for project in first_page:
do_something_with(project)
```
Now lets suppose that you get the next page:
```python
next_page = first_page.next()
for project in next_page:
do_something_with(project)
```
Now, lets inspect the params of the first page:
```python
first_page._params # <<< {'page[cursor]': "..."}
```
Here is the problem: `next` does this
```python
def next(self):
return self.__class__(self.API, self.next_url, self._params)
```
`self.__class__()` will call `__init__` which will do:
```python
def __init__(self, API, url, params=None):
...
query_params = ...
params.update(query_params)
...
```
There are two problems here:
- The first one is that the `params.update(...)` call will modify the
params variable which is a reference to the `._params` attribute of the
**first** page. This explains the bug. We should make sure that the
initializer modifies a copy of the parameters to protect from this
issue.
- The second problem is that the first page's `.next()` call has no
business providing (a copy of) its parameters to the initializer; it's
the job of the server to include any parameters in the pagination URL
itself.
This can also occur if you use `.all()` or `.all_pages()` (and if there
are more than one pages) before using the parameters of the first page
again, for example if you want to apply a filter to the collection.1 parent 60cd3a3 commit e8780f8
1 file changed
Lines changed: 9 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
58 | | - | |
59 | | - | |
| 60 | + | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| |||
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
72 | | - | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
79 | | - | |
| 78 | + | |
80 | 79 | | |
81 | 80 | | |
82 | 81 | | |
| |||
104 | 103 | | |
105 | 104 | | |
106 | 105 | | |
107 | | - | |
108 | | - | |
| 106 | + | |
109 | 107 | | |
110 | 108 | | |
111 | 109 | | |
| |||
125 | 123 | | |
126 | 124 | | |
127 | 125 | | |
128 | | - | |
| 126 | + | |
129 | 127 | | |
130 | 128 | | |
131 | 129 | | |
132 | 130 | | |
133 | 131 | | |
134 | | - | |
| 132 | + | |
135 | 133 | | |
136 | 134 | | |
137 | 135 | | |
| |||
153 | 151 | | |
154 | 152 | | |
155 | 153 | | |
156 | | - | |
157 | | - | |
| 154 | + | |
158 | 155 | | |
159 | 156 | | |
160 | 157 | | |
| |||
0 commit comments