swagger-api / swagger-api/swagger-codegen

[Csharp] - ApiClient.mustache ParameterToString fail if client is not US localized

Open Beginner friendly
#9,413 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

ParameterToString does not create a correct value to be serialized.
The numeric values created to be inserted in a json or xml must have the point as a decimal separator.
If the client is localized with a language that includes a comma as the decimal separator (France, Italy and other European countries) the ToString conversion that occurs inside the else statment uses a comma as the decimal separator.
At this point the serialization and json creation attempts fail.

It is necessary to force, for the numerical values, a ToString conversion with US localization that will have the dot as a separator even if the client is set in a different way.

Swagger-codegen version

2.4.5

Swagger declaration file content or url
Command line used for generation
Steps to reproduce

In the client settings, select a country that uses the comma as a decimal separator and make a call to the Csharp SDK that returns numeric values.

Related issues/PRs
Suggest a fix/enhancement
        public string ParameterToString(object obj)
        {
            if (obj is DateTime)
                // Return a formatted date string - Can be customized with Configuration.DateTimeFormat
                // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
                // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
                // For example: 2009-06-15T13:45:30.0000000
                return ((DateTime)obj).ToString (Configuration.DateTimeFormat);
            else if (obj is DateTimeOffset)
                // Return a formatted date string - Can be customized with Configuration.DateTimeFormat
                // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
                // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
                // For example: 2009-06-15T13:45:30.0000000
                return ((DateTimeOffset)obj).ToString (Configuration.DateTimeFormat);
            else if (obj is IList)
            {
                var flattenedString = new StringBuilder();
                foreach (var param in (IList)obj)
                {
                    if (flattenedString.Length > 0)
                        flattenedString.Append(",");
                    flattenedString.Append(param);
                }
                return flattenedString.ToString();
            }
            else if (obj is double || obj is decimal)
            {
                return Convert.ToString(obj, new CultureInfo("en-US"));
            }
            else
                return Convert.ToString (obj);
        }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the C# ApiClient.mustache template and locate ParameterToString. Reproduce the generated SDK call under a locale that uses a comma decimal separator, then verify that numeric values serialize with a dot while date, list, and other parameter handling remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.