Skip to content

Commit

Permalink
.Net: Invariant culture for OpenApiSchema conversion (#4540)
Browse files Browse the repository at this point in the history
Closes : #4504

### Motivation, Context and Description
The issue occurred during the conversion from OpenApiSchema to
KernelJsonSchema on environments with a culture that uses a comma as a
decimal separator. To resolve this, the
OpenApiSchemaExtensions.ToJsonSchema method has been updated to utilize
the invariant culture for conversion instead of using current culture.

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
  • Loading branch information
SergeyMenshykh authored Jan 10, 2024
1 parent f337261 commit a843f47
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Globalization;
using System.IO;
using System.Text;
using Microsoft.OpenApi.Models;
Expand All @@ -17,7 +18,7 @@ internal static class OpenApiSchemaExtensions
internal static KernelJsonSchema ToJsonSchema(this OpenApiSchema schema)
{
var schemaBuilder = new StringBuilder();
var jsonWriter = new OpenApiJsonWriter(new StringWriter(schemaBuilder));
var jsonWriter = new OpenApiJsonWriter(new StringWriter(schemaBuilder, CultureInfo.InvariantCulture));
jsonWriter.Settings.InlineLocalReferences = true;
schema.SerializeAsV3(jsonWriter);
return KernelJsonSchema.Parse(schemaBuilder.ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Collections.Generic;
using System.Globalization;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.SemanticKernel.Plugins.OpenApi;
using Xunit;

namespace SemanticKernel.Functions.UnitTests.OpenApi.Extensions;
public class OpenApiSchemaExtensionsTests
{
[Fact]
public void ItShouldConvertOpenApiSchemaUsingInvariantCulture()
{
// Arrange
var schema = new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>
{
["property1"] = new OpenApiSchema
{
Type = "number",
Format = "double",
Default = new OpenApiDouble(12.01)
}
}
};

var currentCulture = CultureInfo.CurrentCulture; // Backup current culture

// Act & Assert
try
{
CultureInfo.CurrentCulture = new CultureInfo("fr-FR"); // French culture uses comma as decimal separator

var result = OpenApiSchemaExtensions.ToJsonSchema(schema); // Should use invariant culture

Assert.True(result.RootElement.TryGetProperty("properties", out var properties));
Assert.True(properties.TryGetProperty("property1", out var property2));
Assert.Equal(12.01, property2.GetProperty("default").GetDouble());
}
finally
{
CultureInfo.CurrentCulture = currentCulture; // Restore current culture
}
}
}

0 comments on commit a843f47

Please sign in to comment.