Skip to content

Use the configured Culture when XmlDeserializer converts primitive values - #2428

Open
menes97 wants to merge 1 commit into
restsharp:devfrom
menes97:fix/xml-deserializer-primitive-culture
Open

menes97 wants to merge 1 commit into
restsharp:devfrom
menes97:fix/xml-deserializer-primitive-culture

Conversation

@menes97

@menes97 menes97 commented Nov 18, 2026

Copy link
Copy Markdown

Description

XmlDeserializer.Culture defaults to CultureInfo.InvariantCulture, but primitive properties and primitive list items ignored it: they were converted with Convert.ChangeType(source, newType), which uses the current culture. So with the default settings, under tr-TR or de-DE <Double>1234.5</Double> silently deserializes to 12345, and under fr-FR it throws a FormatException. XmlAttributeDeserializer goes through the same code.

The internal ChangeType extension now takes an IFormatProvider, and both callers in XmlDeserializer pass Culture.

Tests in XmlDeserializerTests:

  • Should_deserialize_floating_point_numbers_with_invariant_culture_by_default (theory over tr-TR, de-DE, fr-FR as CurrentCulture): a double, a float and a List<double>.
  • Should_deserialize_floating_point_numbers_with_configured_culture: Culture = de-DE with comma input while CurrentCulture is invariant.

All 4 cases fail without the change (e.g. Expected: 1234,5 Actual: 12345, and the FormatException for fr-FR) and pass with it. RestSharp.Tests.Serializers.Xml passes on net10.0 and net8.0 (115 tests), RestSharp.Tests on net10.0 (275). I did not run the net48/net9.0 targets or the integrated tests.

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)

This change was written with Claude Code.

🤖 Generated with Claude Code

…lues

XmlDeserializer.Culture (invariant by default) was ignored for primitive
properties and list items, which went through Convert.ChangeType with the
current culture. Under tr-TR, de-DE, etc. "1234.5" became 12345 for a double,
or threw a FormatException (fr-FR).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qodo-free-for-open-source-projects

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Locale-formatted numbers that used to parse now throw 🐞 Bug ≡ Correctness
Description
XmlDeserializer now converts primitive properties and list items with Culture (invariant by
default) instead of the thread's current culture. Payloads that were formatted for the machine's
locale, such as 1,5 under de-DE, used to deserialize correctly and now fail with a
FormatException or are read differently, with no opt-in from the caller.
Code

src/RestSharp.Serializers.Xml/XmlDeserializer.cs[182]

+                    prop.SetValue(x, value.ChangeType(asType, Culture), null);
Evidence
The Culture default is CultureInfo.InvariantCulture, and both primitive paths now pass it to
ChangeType. Any existing consumer whose server emits locale-specific decimal text on a
non-invariant machine changes behavior without changing its configuration. The PR description calls
this a non-breaking bugfix, but it changes deserialization results for those users.

src/RestSharp.Serializers.Xml/XmlDeserializer.cs[27-27]
src/RestSharp.Serializers.Xml/XmlDeserializer.cs[447-447]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

Issue description
Primitive values in XmlDeserializer are now parsed with Culture (invariant by default) instead of the current culture. Existing users whose XML contains locale-formatted numbers on non-invariant machines will start to fail or read different values, but the PR is marked non-breaking.

Fix Focus Areas
- src/RestSharp.Serializers.Xml/XmlDeserializer.cs[182-182]
- src/RestSharp.Serializers.Xml/XmlDeserializer.cs[447-447]

Recommended Fix
Mark the PR as a behavior change in the release notes or changelog, and state that users relying on the current culture should set XmlDeserializer.Culture explicitly. Consider a note in the Culture property XML docs. Do not add a fallback to the current culture, because that would reintroduce the bug.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context sources
Review mode: Auto: 🚀 Fast: Localized culture-conversion fix with focused tests and limited behavioral scope.

Grey Divider

Tip of the day
💡 Did you know, you can type 'qodo, fix this' on a finding and the fix lands right on your PR

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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.

1 participant