Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetQueryParams with collection of KV pairs, duplicate keys should append, not overwrite #370

Closed
tmenier opened this issue Sep 5, 2018 · 6 comments

Comments

@tmenier
Copy link
Owner

tmenier commented Sep 5, 2018

Flurl allows setting multiple query parameters via object notation, with special handling for things that look like collections of key/value pairs. It's possible that these collections could contain duplicate keys, and in this case every value should be appended. Instead they get overwritten each time, so this test fails:

var url = "http://foo.com".SetQueryParams(new[] {
    new { key = "x", value = 1 },
    new { key = "x", value = 2 },
});

Assert.AreEqual("http://foo.com?x=1&x=2", url.ToString());
// fail! actual value is http://foo.com?x=2

In a nutshell, that should pass.

@tmenier tmenier added this to the Flurl 2.8.1 milestone Sep 5, 2018
@tmenier tmenier added the bug label Sep 5, 2018
@Marusyk
Copy link
Contributor

Marusyk commented Sep 26, 2018

It seems to be a breaking changes. Could you please advise if you wanna replace that logic or add some parameter for that? Then I'll try to prepare PR. Thx

@tmenier
Copy link
Owner Author

tmenier commented Sep 29, 2018

@Marusyk Sometimes it's a fine line between a breaking change and fixing some unintended behavior seems "obviously" quirky/buggy. I logged this because I bumped into it in a real-world scenario and it seemed "obviously" wrong, but I'm now wondering if the likelihood of breaking someone is higher than I thought. Tough call.

Keep in mind this DOES overwrite, which is intentional and won't change:

url
    .SetQueryParam("x", 1)
    .SetQueryParam("x", 2);

I intentionally used the word "Set" instead of "Add" or "Append" here to help make it clear that you're providing the entire value so go ahead and squash whatever might already be there. If you want x=1&x=2, you can always do SetQueryParam("x", new[] { 1, 2 }).

The scenario in this issue is more unusual. If you use SetQueryParams with object notation or a dictionary, you can't have duplicate keys, so it doesn't apply there either. This is only if you pass in some collection of key/value pairs that does have duplicate keys. In my mind, you're saying you want all of them, and overwriting is unintuitive. But, maybe it's unusual enough that the risk of breaking someone is not worth changing it? I'm not sure.

@tmenier tmenier removed this from the Flurl 2.8.1 milestone Sep 29, 2018
@tmenier tmenier changed the title Url.SetQueryParams with multiple values, same key should append, not overwrite Url.SetQueryParams with collection of KV pairs, duplicate keys should append, not overwrite Sep 29, 2018
@tmenier tmenier changed the title Url.SetQueryParams with collection of KV pairs, duplicate keys should append, not overwrite SetQueryParams with collection of KV pairs, duplicate keys should append, not overwrite Sep 29, 2018
@rcollette
Copy link

In the case where all the values are passed in the single set method and squashed, it seems like an obvious bug to be patched.

@tmenier tmenier added next up and removed next up labels Jan 18, 2019
tmenier added a commit that referenced this issue Jan 19, 2019
@tmenier
Copy link
Owner Author

tmenier commented Jan 19, 2019

I was hoping I could piggyback off existing code to fix this but it's a little trickier than I thought. I'm considering adding AppendQueryParam(s) that, unlike SetQueryParam(s), don't overwrite. With that in place this will be much easier.

@tmenier tmenier added this to the Flurl 2.9 milestone Jan 19, 2019
@tmenier tmenier added reviewed and removed next up labels Jan 19, 2019
@tmenier tmenier added 3.0 and removed reviewed labels Mar 24, 2019
@tmenier tmenier removed this from the Flurl 3.0 milestone Oct 1, 2020
@tmenier tmenier removed the 3.0 label Nov 1, 2020
@tmenier tmenier added the 4.0 label May 13, 2022
@tmenier
Copy link
Owner Author

tmenier commented May 13, 2022

Although I still think this seems like an obvious quirk, now that I'm pushing it out to 4.0 I'll play it safe and call it breaking. #688 will be a prerequisite.

tmenier pushed a commit that referenced this issue Sep 15, 2023
@tmenier
Copy link
Owner Author

tmenier commented Oct 9, 2023

Now available in prerelease 4 https://www.nuget.org/packages/Flurl/4.0.0-pre4

@tmenier tmenier closed this as completed Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Released
Development

No branches or pull requests

3 participants