Skip to content

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

Open
@mhoumann

Description

@mhoumann

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions