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

Wrong string representation of Parameter #2035

Closed
hmG3 opened this issue Mar 25, 2023 · 1 comment
Closed

Wrong string representation of Parameter #2035

hmG3 opened this issue Mar 25, 2023 · 1 comment
Labels

Comments

@hmG3
Copy link

hmG3 commented Mar 25, 2023

Describe the bug
After upgrading from version 106 to 109 I noticed some of our logic is broken.
We are using an expression to build query strings like

String.Join('&', request.Parameters.Where(p => p.Type == ParameterType.GetOrPost));

which calls ToString() method for each parameter object, and we should get the resulting string like param1=value1&param2=value2.

Even though there is already a proper override in the base abstract class

public override string ToString() => $"{Name}={Value}";

it doesn't work for all derived record classes of Parameter because each record class creates its own override of ToString() hiding the one from the base class.

A possible solution here might be adding sealed modifier to stop the compiler from synthesizing new overrides:

public sealed override string ToString() => $"{Name}={Value}";

To Reproduce

var request = new RestRequest("api/test");
request.AddParameter("param1", "value1");
request.AddParameter("param2", "value2");
var queryString = String.Join('&', request.Parameters);
Assert.Equal("param1=value1&param2=value2", queryString); //fail
@alexeyzimarev
Copy link
Member

Ok I sealed the ToString() but also added an extension for IRestClient to get the query string. It's called GetRequestQuery. It also considers default query parameters and does all the necessary encodings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants