diff --git a/generator/ServiceClientGeneratorLib/Example.cs b/generator/ServiceClientGeneratorLib/Example.cs index 60891d8eb0fd..7c801629d1de 100644 --- a/generator/ServiceClientGeneratorLib/Example.cs +++ b/generator/ServiceClientGeneratorLib/Example.cs @@ -42,7 +42,7 @@ public Operation Operation /// The example id taken from the model. /// /// - /// This unique id is used for the region in the emitted code sample + /// This unique id is used for the region in the emitted code sample /// that will be parsed to include the code in the documentation. /// public string Id @@ -201,7 +201,7 @@ public IList GetResponseAssignments() return result; } - + /// /// Given a member and sample data, build a literal/instantation for the @@ -227,9 +227,15 @@ public void GetSampleLiteral(Shape shape, JsonData data, CodeBuilder cb) if (shape.IsString && data.IsString) cb.AppendQuote(data.ToString()); else if (shape.IsBoolean) - cb.Append(data.ToString().ToLower()); + cb.Append(data.ToString().ToLowerInvariant()); + else if (shape.IsFloat || shape.IsInt || shape.IsDouble || shape.IsLong) - cb.Append(data.ToString()); + { + if (data.IsDouble) + cb.Append(((double)data).ToString(CultureInfo.InvariantCulture)); + else + cb.Append(data.ToString()); + } else if (shape.IsList && data.IsArray) { @@ -285,7 +291,7 @@ public void GetSampleLiteral(Shape shape, JsonData data, CodeBuilder cb) foreach (var field in data.PropertyNames) { var property = shape.Members.GetMemberByName(field); - + if (null == property) continue; diff --git a/generator/ServiceClientGeneratorTests/UnitTests.cs b/generator/ServiceClientGeneratorTests/UnitTests.cs index 7322f897c612..f318222d2b1a 100644 --- a/generator/ServiceClientGeneratorTests/UnitTests.cs +++ b/generator/ServiceClientGeneratorTests/UnitTests.cs @@ -5,6 +5,9 @@ using System.Reflection; using Xunit; using System.Linq; +using System.Text; +using System.Globalization; +using System.IO; namespace ServiceClientGeneratorTests { @@ -66,5 +69,39 @@ public void TestEndpointNameConstruction(string regionCode, string expectedName) Assert.Equal(GeneratorDriver.ConstructEndpointName(regionCode), expectedName); } + [Theory] + [InlineData("string", "str", "\"str\"")] + [InlineData("integer", 5, "5")] + [InlineData("float", 0.4, "0.4")] + [InlineData("double", 0.5, "0.5")] + public void TestExampleShapes(string shapeType, object value, string expectedText) + { + TextReader tr = new StringReader("{}"); + ServiceModel testModel = new(tr, null); + + JsonData shapeJsonData = new() + { + ["type"] = shapeType + }; + + var sb = new StringBuilder(); + var cb = new CodeBuilder(sb, 0); + + string operationName = "fake"; + Example example = new(testModel, operationName, null); + + Shape shape = Shape.CreateShape(testModel, operationName, shapeJsonData); + JsonData sampleData = new(value); + + CultureInfo originalCulture = CultureInfo.CurrentCulture; + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR"); // In French, the comma is used as a decimal point + + example.GetSampleLiteral(shape, sampleData, cb); + + CultureInfo.CurrentCulture = originalCulture; + + var printedText = sb.ToString(); + Assert.Equal(expectedText, printedText); + } } }