Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Correct query loss when using parse_qsl to dict #622

Merged
merged 1 commit into from
Aug 17, 2016

Conversation

dhermes
Copy link
Contributor

@dhermes dhermes commented Aug 11, 2016

NOTE: I noticed this when doing a fairly comprehensive source check to find the httplib2 footprint (for #128).

The issue is that repeated params could just be dropped:

>>> from six.moves import urllib
>>> params_list = urllib.parse.parse_qsl('a=1&a=2')
>>> params_list
[('a', '1'), ('a', '2')]
>>> dict(params_list)
{'a': '2'}

@theacodes
Copy link
Contributor

This LGTM, but I wonder if there's not already something that handles this well?

@dhermes
Copy link
Contributor Author

dhermes commented Aug 11, 2016

I just did git grep parse_q. It's crazy it was used incorrectly in 4 different places.

@dhermes
Copy link
Contributor Author

dhermes commented Aug 11, 2016

@jonparrott I want to hold off on the merge until @nathanielmanistaatgoogle weighs in

@theacodes
Copy link
Contributor

@dhermes good plan. This seems like hackery I'm not sure I like.

@dhermes
Copy link
Contributor Author

dhermes commented Aug 11, 2016

This seems like hackery I'm not sure I like.

What is "this"?

@theacodes
Copy link
Contributor

Doing urlparsing and such.

@dhermes
Copy link
Contributor Author

dhermes commented Aug 11, 2016

Yeah I dunno. You can file an issue to follow-up?

@theacodes
Copy link
Contributor

Just more to think about before we close #128 I think.

@dhermes
Copy link
Contributor Author

dhermes commented Aug 11, 2016

Currently typing one-handed w/baby in my arms, can you add a checkbox about it in the long desc. for #128 for me?

@theacodes
Copy link
Contributor

I read that as "typing with baby arms". :D

Will do.

@dhermes dhermes mentioned this pull request Aug 11, 2016
7 tasks
@@ -199,12 +199,7 @@ def _add_query_parameter(url, name, value):
"""
if value is None:

This comment was marked as spam.

@dhermes
Copy link
Contributor Author

dhermes commented Aug 15, 2016

@nathanielmanistaatgoogle PTAL.

In particular, we should resolve what we think the actual goal of all those dict(parse_qsl(...)) calls were.

@nathanielmanistaatgoogle
Copy link
Contributor

Agreed that this needs a deeper look; probably site-by-site (luckily there are few). Replace-except-if-the-value-is-a-list-of-two-or-more-then-add is just weird semantics.

If it's the case that all sites in the library are correct with replace semantics, then our library-private _helpers module should make available a helper function that implements replace semantics.

If it's the case that all sites in the library are correct with add-to semantics, then our library-private _helpers module should make available a helper function that implements add-to semantics.

If it's the case that some sites in the library are correct with replace semantics and some are correct with add-to semantics, then our library-private _helpers module should make available a helper function that implements replace semantics and a helper function that implements add-to semantics.

I seriously doubt that it's the case that there's a site in the library where the correct semantics are replace-except-if-the-value-is-a-list-of-two-or-more-then-add.

Am I making sense?

@dhermes
Copy link
Contributor Author

dhermes commented Aug 17, 2016

Yes. But the issue with replace is if the original had multiple values, then what does replace mean? Which is why I suggested we first verify that each parameter shows up exactly once, then replace.

i.e. a=1&b=2 replace b=3 (or c=3) is easy but what if the original was b=1&b=2

@nathanielmanistaatgoogle
Copy link
Contributor

For private helper methods, replace means what we implement it to mean. I could see it going one of two ways: either "replace" semantics mean "associate this key with this value, no matter whether zero, one, two, or more values were associated with the key previously" or "replace" semantics mean "associate this key with this value, and if there were zero values with the key previously, that's fine, and if there was one value with the key previously, that's also fine, but if there were two or more values associated with the key previously, that's a problem so raise an exception". While either of these choices would be acceptable to be blessed as correct, I suspect it's the first that is going to be more useful and applicable in our codebase. Is my suspicion correct?

@dhermes dhermes force-pushed the allow-repeated-params branch 2 times, most recently from ced809d to 15033c1 Compare August 17, 2016 22:23
@dhermes
Copy link
Contributor Author

dhermes commented Aug 17, 2016

@nathanielmanistaatgoogle Hopefully this works. Don't forget to remind me to squash on LGTM

@nathanielmanistaatgoogle
Copy link
Contributor

Looks good (after out-of-band discussion); squash and merge.

@dhermes dhermes merged commit 51ae876 into googleapis:master Aug 17, 2016
@dhermes dhermes deleted the allow-repeated-params branch August 17, 2016 22:59
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants