fix: use the [] after key names for array variables in params#1699
fix: use the [] after key names for array variables in params#1699
params#1699Conversation
1572d87 to
0f0f823
Compare
98172f6 to
f596068
Compare
f596068 to
1591a91
Compare
Codecov Report
@@ Coverage Diff @@
## main #1699 +/- ##
==========================================
- Coverage 95.54% 95.53% -0.01%
==========================================
Files 81 81
Lines 5296 5309 +13
==========================================
+ Hits 5060 5072 +12
- Misses 236 237 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
1591a91 to
76b04a8
Compare
|
|
db3ab50 to
e3c2e95
Compare
|
@nejch This is ready for review now. Interested to hear your feedback. Thanks. |
e3c2e95 to
fdc854a
Compare
b44cef7 to
902d9f6
Compare
3244596 to
2bc7fad
Compare
81d8c46 to
f7e0842
Compare
|
I finally had some more time to look at this in more detail! I have some thoughts to maybe simplify this. We only need to do encoding when sending data as query parameters, not as a payload. So I think the encoding part is relevant for GET requests, as we can use json-encoded data as provided otherwise. I just tested this with GitLab's docs suggest this too:
So https://docs.gitlab.com/ee/api/#encoding-api-parameters-of-array-and-hash-types is a bit misleading here because they don't use And I'm thinking that complex data structures only make sense for POST/PUT requests, while arrays are still useful for GET for list filters, for example. So we only need to encode arrays properly for query params, IMO. This would really simplify things as all we need to do is transform e.g. E.g.: >>> from urllib.parse import urlencode
>>> urlencode({"scopes": ["api", "read_user"]}, doseq=True)
'scopes=api&scopes=read_user'
>>> urlencode({"scopes[]": ["api", "read_user"]}, doseq=True)
'scopes%5B%5D=api&scopes%5B%5D=read_user'I feel like we could avoid some of the conversions happening here if we do it like that. WDYT @JohnVillalovos? |
|
Should we try to revive this one @JohnVillalovos? Seems like an important fix. Probably fixes a few more issues down the line, will need to check those. |
Yeah, probably. Let me see if I get some time this weekend. Thanks for reminding me! |
f7e0842 to
a939db1
Compare
7f6a8c2 to
50b1f12
Compare
50b1f12 to
2ca7ddb
Compare
|
@nejch It is ready for review again. Thanks for your patience. |
There was a problem hiding this comment.
Thanks @JohnVillalovos looks super simple now. I have some concerns but I think I'll just merge this and use it to track and open a refactor based on my comments.
I have to take that back - seems like this also transforms the key for POST/PUT payloads which shouldn't happen. This maybe needs a test with create() to ensure we don't use key[] in payloads.
Edit2: I think we need to maybe change _transform_types to take another argument (transform_keys) which we pass in in the ListMixin or maybe even directly in get requests.
I think we need to transform "parameters'. Whether it is a GET/POST/PUT. Now handling values in the |
Yes, that's right :) sorry for the confusion. payload = no transform, query params = transform. |
2ca7ddb to
ba084c6
Compare
|
@JohnVillalovos I think this doesn't work quite right yet. Can you try this test in this patch? diff --git a/tests/unit/mixins/test_mixin_methods.py b/tests/unit/mixins/test_mixin_methods.py
index 69cdb78..e8870b9 100644
--- a/tests/unit/mixins/test_mixin_methods.py
+++ b/tests/unit/mixins/test_mixin_methods.py
@@ -314,6 +314,26 @@ def test_create_mixin_custom_path(gl):
assert responses.assert_call_count(url, 1) is True
+
+@responses.activate
+def test_create_mixin_with_attributes(gl):
+ class M(CreateMixin, FakeManager):
+ _types = {"my_array": gl_types.ArrayAttribute}
+
+ url = "http://localhost/api/v4/tests"
+ responses.add(
+ method=responses.POST,
+ headers={},
+ url=url,
+ json={},
+ status=200,
+ match=[responses.matchers.json_params_matcher({"my_array": ["1", "2", "3"]})],
+ )
+
+ mgr = M(gl)
+ mgr.create({"my_array": [1, 2, 3]})
+
+
def test_update_mixin_missing_attrs(gl):
class M(UpdateMixin, FakeManager):
_update_attrs = gl_types.RequiredOptional(
The reason we might want this is, for example |
ba084c6 to
bc6ddf2
Compare
Thanks @nejch I totally missed that the Updated the code to only modify the data for the |
1. If a value is of type ArrayAttribute then append '[]' to the name of the value for query parameters (`params`). This is step 3 in a series of steps of our goal to add full support for the GitLab API data types[1]: * array * hash * array of hashes Step one was: commit 5127b15 Step two was: commit a57334f Fixes: #1698 [1] https://docs.gitlab.com/ee/api/#encoding-api-parameters-of-array-and-hash-types
of the value.
This is step 3 in a series of steps of our goal to add full
support for the GitLab API data types[1]:
Step one was: commit 5127b15
Step two was: commit a57334f
Fixes: #1698
Related to #780
Closes #806
[1] https://docs.gitlab.com/ee/api/#encoding-api-parameters-of-array-and-hash-types