Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT license.

using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
Expand All @@ -25,7 +25,6 @@ namespace Microsoft.OpenApi
/// </remarks>
public sealed class OpenApiSchemaJsonConverter : JsonConverter<OpenApiSchema>
{
private static readonly UTF8Encoding Utf8NoBom = new(encoderShouldEmitUTF8Identifier: false);
private readonly OpenApiSpecVersion _version;

/// <summary>
Expand Down Expand Up @@ -67,7 +66,10 @@ public override void Write(Utf8JsonWriter writer, OpenApiSchema value, JsonSeria
Utils.CheckArgumentNull(value);

using var stream = new MemoryStream();
using (var textWriter = new StreamWriter(stream, Utf8NoBom, bufferSize: 1024, leaveOpen: true))
// Numbers must be written with the invariant culture: under de-DE the writer would emit 1.5 as "1,5",
// which the raw value below would pass through as invalid JSON.
// The writer emits UTF-8 without a BOM; disposing it closes the stream, which ToArray still reads.
using (var textWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture))
{
var openApiWriter = new OpenApiJsonWriter(textWriter);
SerializeSchema(value, openApiWriter);
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#if NET
using System.Collections.Immutable;
#endif
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
Expand Down Expand Up @@ -570,7 +571,7 @@ public async Task<string> GetHashCodeAsync(CancellationToken cancellationToken =
{
#if NET7_OR_GREATER
using var memoryStream = new MemoryStream();
using var streamWriter = new StreamWriter(memoryStream);
using var streamWriter = new FormattingStreamWriter(memoryStream, CultureInfo.InvariantCulture);

await WriteDocumentAsync(streamWriter, cancellationToken).ConfigureAwait(false);

Expand All @@ -580,7 +581,8 @@ public async Task<string> GetHashCodeAsync(CancellationToken cancellationToken =
#else
using HashAlgorithm sha = SHA512.Create();
using var cryptoStream = new CryptoStream(Stream.Null, sha, CryptoStreamMode.Write);
using var streamWriter = new StreamWriter(cryptoStream);
// The invariant culture keeps the hash of a document the same on every machine
using var streamWriter = new FormattingStreamWriter(cryptoStream, CultureInfo.InvariantCulture);

await WriteDocumentAsync(streamWriter, cancellationToken).ConfigureAwait(false);
#if NET5_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
using Xunit;

namespace Microsoft.OpenApi.Tests.Converters
Expand All @@ -21,6 +23,35 @@ public class OpenApiSchemaJsonConverterTests
Converters = { new OpenApiSchemaJsonConverter() }
};

[Theory]
[InlineData("de-DE")] // writes 1.5 as "1,5"
[InlineData("fi-FI")] // writes -5 with U+2212 MINUS SIGN
public void Serialize_NumericValues_DoNotDependOnCurrentCulture(string culture)
{
var schema = new OpenApiSchema
{
Type = JsonSchemaType.Number,
Default = JsonValue.Create(1.5),
Enum = [JsonValue.Create(-5), JsonValue.Create(1.5)]
};

var originalCulture = CultureInfo.CurrentCulture;
string json;
try
{
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
json = JsonSerializer.Serialize(schema, _optionsV31);
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
}

using var doc = JsonDocument.Parse(json);
Assert.Equal(1.5, doc.RootElement.GetProperty("default").GetDouble());
Assert.Equal(-5, doc.RootElement.GetProperty("enum")[0].GetInt32());
}

[Fact]
public void Serialize_SimpleStringSchema_ProducesOpenApiWireFormat()
{
Expand Down
33 changes: 33 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,39 @@ public async Task SerializeRelativeRootPathWithHostAsV2JsonWorks()
Assert.Equal(expected, actual);
}

[Fact]
public async Task HashCodeDoesNotDependOnCurrentCulture()
{
var document = new OpenApiDocument
{
Info = new OpenApiInfo { Title = "Culture", Version = "1.0" },
Components = new OpenApiComponents
{
Schemas = new Dictionary<string, IOpenApiSchema>
{
["price"] = new OpenApiSchema { Type = JsonSchemaType.Number, Default = JsonValue.Create(1.5) }
}
}
};

var originalCulture = CultureInfo.CurrentCulture;
string invariantHash, germanHash;
try
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
invariantHash = await document.GetHashCodeAsync(TestContext.Current.CancellationToken);
// de-DE writes 1.5 as "1,5"
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
germanHash = await document.GetHashCodeAsync(TestContext.Current.CancellationToken);
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
}

Assert.Equal(invariantHash, germanHash);
}

[Fact]
public async Task TestHashCodesForSimilarOpenApiDocuments()
{
Expand Down