Skip to content

Conversation

Copy link

Copilot AI commented Nov 28, 2025

Description

Adds a CultureForParameters property to RestClientOptions that allows configuring the culture used when formatting IFormattable parameter values (like doubles, decimals, dates). This addresses the issue where values like 1.234 are formatted as "1,234" in locales that use comma as decimal separator (e.g., da-DK), breaking API compatibility.

// Configure client to use invariant culture for parameter formatting
var options = new RestClientOptions { CultureForParameters = CultureInfo.InvariantCulture };
using var client = new RestClient(options);

// Add parameter using client's culture setting
var request = new RestRequest();
request.AddParameter(client, "value", 1.234); // "1.234" with InvariantCulture

// Or add default parameters
client.AddDefaultParameter("value", 1.234); // Uses client's CultureForParameters

Changes:

  • Added CultureForParameters property to RestClientOptions (defaults to null which uses local culture)
  • Added ObjectExtensions.ToStringWithCulture<T>() - uses IFormattable.ToString(null, culture) for formattable types, falls back to ToString() otherwise
  • Added generic overloads to RestClientExtensions for adding default parameters with culture formatting:
    • AddDefaultParameter<T>(name, value)
    • AddDefaultQueryParameter<T>(name, value)
    • AddDefaultUrlSegment<T>(name, value)
    • FormatValue<T>(value) helper method
  • Added new extension methods in RestRequestExtensions.Culture.cs for adding parameters to requests using client's culture:
    • AddParameter<T>(request, client, name, value, encode)
    • AddOrUpdateParameter<T>(request, client, name, value, encode)
    • AddQueryParameter<T>(request, client, name, value, encode)
    • AddUrlSegment<T>(request, client, name, value, encode)
  • Default behavior remains unchanged (uses current culture) to avoid breaking changes

Fixes #2270

Purpose

This pull request is a:

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
Original prompt

This section details on the original issue you should resolve

<issue_title>RestRequestExtensions.AddParameter and AddOrUpdateParameter should use CultureInfo.InvariantCulture for IFormattable types</issue_title>
<issue_description>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</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…er methods

Co-authored-by: alexeyzimarev <2821205+alexeyzimarev@users.noreply.github.com>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 28, 2025

Deploying restsharp with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1fb26de
Status: ✅  Deploy successful!
Preview URL: https://aee672ce.restsharp.pages.dev
Branch Preview URL: https://copilot-fix-cultureinfo-para.restsharp.pages.dev

View logs

Copilot AI changed the title [WIP] Fix AddParameter to use CultureInfo.InvariantCulture for IFormattable types Use CultureInfo.InvariantCulture for IFormattable types in AddParameter methods Nov 28, 2025
Copilot AI requested a review from alexeyzimarev November 28, 2025 19:03
Copilot finished work on behalf of alexeyzimarev November 28, 2025 19:03
Copilot AI mentioned this pull request Nov 29, 2025
5 tasks
@alexeyzimarev
Copy link
Member

@copilot some tests failed so it must be a breaking change. it'd be good to make the new behaviour to be opt-in.

Co-authored-by: alexeyzimarev <2821205+alexeyzimarev@users.noreply.github.com>
Copy link
Author

Copilot AI commented Nov 29, 2025

@copilot some tests failed so it must be a breaking change. it'd be good to make the new behaviour to be opt-in.

Made the invariant culture formatting opt-in by adding new method overloads with a useInvariantCulture parameter. The default behavior remains unchanged (uses current culture). Users can opt-in via:

request.AddParameter("value", 1.234, useInvariantCulture: true);

Commit: 8059e9b

Copilot AI changed the title Use CultureInfo.InvariantCulture for IFormattable types in AddParameter methods Add opt-in CultureInfo.InvariantCulture support for IFormattable types in AddParameter methods Nov 30, 2025
Copilot finished work on behalf of alexeyzimarev November 30, 2025 00:04
@alexeyzimarev
Copy link
Member

@copilot Instead of new parameter, add a property to RestClientOptions to set the culture. Use the default value that would make the default behaviour unchanged (local culture).

Copilot AI and others added 2 commits November 30, 2025 07:57
…e parameter formatting

Co-authored-by: alexeyzimarev <2821205+alexeyzimarev@users.noreply.github.com>
Co-authored-by: alexeyzimarev <2821205+alexeyzimarev@users.noreply.github.com>
Copy link
Author

Copilot AI commented Nov 30, 2025

@copilot Instead of new parameter, add a property to RestClientOptions to set the culture. Use the default value that would make the default behaviour unchanged (local culture).

Added CultureForParameters property to RestClientOptions. When set to CultureInfo.InvariantCulture, parameter values will use invariant formatting. Default is null (uses local culture).

Usage:

var options = new RestClientOptions { CultureForParameters = CultureInfo.InvariantCulture };
using var client = new RestClient(options);

var request = new RestRequest();
request.AddParameter(client, "value", 1.234); // "1.234"
// Or for default parameters:
client.AddDefaultParameter("value", 1.234);

Commit: 1fb26de

Copilot AI changed the title Add opt-in CultureInfo.InvariantCulture support for IFormattable types in AddParameter methods Add CultureForParameters option to RestClientOptions for culture-aware parameter formatting Nov 30, 2025
Copilot finished work on behalf of alexeyzimarev November 30, 2025 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants