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

RestRequestExtensions.AddParameter and AddOrUpdateParameter should use CultureInfo.InvariantCulture for IFormattable types #2270

Open
mhoumann opened this issue Oct 25, 2024 · 0 comments
Labels

Comments

@mhoumann
Copy link

Describe the bug
Using ToString() on e.g, a double value will use current culture to format the value.
The resulting string value is not likely to be parseable at receiving end if current locale uses different decimal separator than standard English dot '.'

To Reproduce

// Set current locale in OS settings - e.g., da-DK

var request = new RestRequest(...)
    .AddParameter("DoubleValue", 1.234);

Expected behavior
String value for parameter should be formatted with invariant culture format if possible.
Expected string value "1.234"
Actual string value: "1,234"

Suggestion:
For instance for IFormattable values, use the IFormattable.ToString(string?, IFormatProvider?) overload

Suggested implementation:
Change value.ToString() to value.ToStringInvariant()

which could be implemented as:

static string? ToStringInvariant<T>(this T value) => value switch
{
    null => null,
    IFormattable f => f.ToString(null, CultureInfo.InvariantCulture),
    _ => value.ToString(),
};

Desktop (please complete the following information):

  • OS: Windows 11
  • .NET version 8
  • Version 112.1
@mhoumann mhoumann added the bug label Oct 25, 2024
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

1 participant