← Winnitude! Search  ·  Reader Mode (2391 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 208 LINKS

fix: use the [] after key names for array variables in `params` by JohnVillalovos · Pull Request #1699 · python-gitlab/python-gitlab

Repository navigation

fix: use the [] after key names for array variables in params - #1699


Merged
nejch merged 1 commit into main from jlvillal/arrays Jul 27, 2022
Merged

fix: use the [] after key names for array variables in params #1699 nejch merged 1 commit into main from jlvillal/arrays

Conversation

JohnVillalovos commented Nov 19, 2021 · edited by nejch Loading



Copy link Copy Markdown
Member
  1. If a value is of type ArrayAttribute then append '[]' to the name
    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]:

  • array
  • hash
  • array of hashes

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

JohnVillalovos force-pushed the jlvillal/arrays branch from 1572d87 to 0f0f823
Compare
November 19, 2021 00:10 JohnVillalovos requested a review from nejch November 19, 2021 00:10 JohnVillalovos force-pushed the jlvillal/arrays branch 7 times, most recently from 98172f6 to f596068
Compare
November 19, 2021 04:22 JohnVillalovos marked this pull request as draft November 19, 2021 04:23 JohnVillalovos force-pushed the jlvillal/arrays branch from f596068 to 1591a91
Compare
November 19, 2021 04:30 JohnVillalovos changed the title WIP: Trying to solve array issue fix: use the [] after key names for array variables Nov 19, 2021

codecov-commenter commented Nov 19, 2021 · edited by codecov
Bot
Loading



Copy link Copy Markdown

Codecov Report

❌ Patch coverage is 96.42857% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.53%. Comparing base (194ee01 ) to head (1af44ce ).
⚠️ Report is 867 commits behind head on main.

Files with missing lines Patch % Lines
gitlab/utils.py 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            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     
Flag Coverage Δ
api_func_v4 81.48% <85.71%> (+<0.01%) ⬆️
cli_func_v4 82.82% <46.42%> (-0.30%) ⬇️
unit 87.28% <92.85%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
gitlab/mixins.py 92.30% <100.00%> (ø)
gitlab/types.py 98.21% <100.00%> (+0.13%) ⬆️
gitlab/utils.py 98.59% <92.85%> (-1.41%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics : Detect flaky tests, report on failures, and find test suite problems.
JohnVillalovos mentioned this pull request Nov 19, 2021 List merge requests using specific approver_ids regression #1698 Closed JohnVillalovos commented Nov 19, 2021
View reviewed changes
JohnVillalovos force-pushed the jlvillal/arrays branch from 1591a91 to 76b04a8
Compare
November 19, 2021 04:45

JohnVillalovos commented Nov 20, 2021 · edited Loading



Copy link Copy Markdown
Member

Author

So in my testing this does not break anything. It also allows approver_ids and approved_by_ids to work for ONE specified user (before it just failed) in listing merge requests. But it does not work for two or more, as it just returns the match for the first one.

I'm not a fan of the silent failure as that is likely worse than current situation where it dies.

JohnVillalovos force-pushed the jlvillal/arrays branch 6 times, most recently from db3ab50 to e3c2e95
Compare
November 20, 2021 21:30 JohnVillalovos marked this pull request as ready for review November 20, 2021 21:30

JohnVillalovos commented Nov 20, 2021



Copy link Copy Markdown
Member

Author

@nejch This is ready for review now. Interested to hear your feedback. Thanks.

JohnVillalovos force-pushed the jlvillal/arrays branch from e3c2e95 to fdc854a
Compare
November 20, 2021 21:56 JohnVillalovos mentioned this pull request Nov 20, 2021 fix: make scopes work with multiple scope-names #1360 Closed JohnVillalovos force-pushed the jlvillal/arrays branch 3 times, most recently from b44cef7 to 902d9f6
Compare
December 1, 2021 01:51 JohnVillalovos force-pushed the jlvillal/arrays branch from 3244596 to 2bc7fad
Compare
March 10, 2022 16:42 JohnVillalovos force-pushed the jlvillal/arrays branch 2 times, most recently from 81d8c46 to f7e0842
Compare
March 27, 2022 14:31

nejch commented Mar 28, 2022



Copy link Copy Markdown
Member

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 topics on project creation, which is an array:
project = gl.projects.create({"name":"test-repo", "visibility": "private", "topics": ["gitlab", "python"]})

GitLab's docs suggest this too:

GET requests usually send a query string, while PUT or POST requests usually send the payload body

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 application/json as they do in the example above that section, and it should be possible for us to do that instead as long as we ensure it is sent as JSON and not as query params. See also #780 (comment) where an array is sent successfully via the API when created as a list. (Of course we still need to later have some basic support hash/array of hashes in order to parse them from the CLI).

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. {"scopes": ["api", "read_user"} into {"scopes[]": ["api", "read_user"}, because requests already does a urlencode there:
https://github.com/psf/requests/blob/8bce583b9547c7b82d44c8e97f37cf9a16cbe758/requests/models.py#L112

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 ?

JohnVillalovos marked this pull request as draft April 16, 2022 18:48

nejch commented Jul 22, 2022



Copy link Copy Markdown
Member

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.

JohnVillalovos commented Jul 22, 2022



Copy link Copy Markdown
Member

Author

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!

nejch mentioned this pull request Jul 23, 2022 Added merge_base API endpoint support #1577 Closed JohnVillalovos force-pushed the jlvillal/arrays branch from f7e0842 to a939db1
Compare
July 24, 2022 16:14 JohnVillalovos marked this pull request as ready for review July 24, 2022 21:13 JohnVillalovos marked this pull request as draft July 24, 2022 21:30 JohnVillalovos force-pushed the jlvillal/arrays branch 2 times, most recently from 7f6a8c2 to 50b1f12
Compare
July 25, 2022 23:55 JohnVillalovos marked this pull request as ready for review July 25, 2022 23:56 JohnVillalovos force-pushed the jlvillal/arrays branch from 50b1f12 to 2ca7ddb
Compare
July 26, 2022 00:11

JohnVillalovos commented Jul 26, 2022



Copy link Copy Markdown
Member

Author

@nejch It is ready for review again. Thanks for your patience.

nejch reviewed Jul 26, 2022
View reviewed changes

nejch left a comment · edited Loading



Copy link Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more .

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.

nejch changed the title fix: use the [] after key names for array variables fix: use the [] after key names for array variables in GET requests Jul 26, 2022

JohnVillalovos commented Jul 26, 2022



Copy link Copy Markdown
Member

Author

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.

I think we need to transform "parameters'. Whether it is a GET/POST/PUT. Now handling values in the data portion is a different thing we need to figure out.

nejch commented Jul 26, 2022



Copy link Copy Markdown
Member

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.

I think we need to transform "parameters'. Whether it is a GET/POST/PUT. Now handling values in the data portion is a different thing we need to figure out.

Yes, that's right :) sorry for the confusion. payload = no transform, query params = transform.

JohnVillalovos force-pushed the jlvillal/arrays branch from 2ca7ddb to ba084c6
Compare
July 27, 2022 15:34 JohnVillalovos requested a review from nejch July 27, 2022 15:35 JohnVillalovos changed the title fix: use the [] after key names for array variables in GET requests fix: use the [] after key names for array variables Jul 27, 2022

nejch commented Jul 27, 2022



Copy link Copy Markdown
Member

@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 scopes as an array in create mixins for #780 .

JohnVillalovos force-pushed the jlvillal/arrays branch from ba084c6 to bc6ddf2
Compare
July 27, 2022 20:58

JohnVillalovos commented Jul 27, 2022 · edited Loading



Copy link Copy Markdown
Member

Author

@JohnVillalovos I think this doesn't work quite right yet. Can you try this test in this patch?

Thanks @nejch I totally missed that the data was not being sent as params in the POST/PUT methods 😔

Updated the code to only modify the data for the list() method.

nejch reviewed Jul 27, 2022
View reviewed changes
fix: use the [] after key names for array variables in params 1af44ce
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
nejch mentioned this pull request Aug 4, 2022 URL-encoded kwargs are infinitely duplicated in query params (v3.7.0 regression for arrays) #2218 Closed nejch mentioned this pull request Aug 14, 2022 feat: add support for merge_base API #2236 Merged Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment

Labels

None yet

3 participants