Skip to content

Commit 2f54a37

Browse files
menes97claude
authored andcommitted
fix: write schema JSON and document hashes with the invariant culture (#3186)
OpenApiSchemaJsonConverter.Write wrote the schema through a StreamWriter, whose format provider is the current culture, and passed the result to Utf8JsonWriter.WriteRawValue without validation. Under de-DE a default of 1.5 came out as "1,5" and under fi-FI -5 as "−5" (U+2212), both invalid JSON. OpenApiDocument.GetHashCodeAsync had the same writer, so a document's hash depended on the machine's culture. Both now use FormattingStreamWriter with CultureInfo.InvariantCulture, like the SerializeAsync extensions already do. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4f8fab2 commit 2f54a37

4 files changed

Lines changed: 73 additions & 5 deletions

File tree

‎src/Microsoft.OpenApi/Converters/OpenApiSchemaJsonConverter.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Globalization;
56
using System.IO;
6-
using System.Text;
77
using System.Text.Json;
88
using System.Text.Json.Nodes;
99
using System.Text.Json.Serialization;
@@ -25,7 +25,6 @@ namespace Microsoft.OpenApi
2525
/// </remarks>
2626
public sealed class OpenApiSchemaJsonConverter : JsonConverter<OpenApiSchema>
2727
{
28-
private static readonly UTF8Encoding Utf8NoBom = new(encoderShouldEmitUTF8Identifier: false);
2928
private readonly OpenApiSpecVersion _version;
3029

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

6968
using var stream = new MemoryStream();
70-
using (var textWriter = new StreamWriter(stream, Utf8NoBom, bufferSize: 1024, leaveOpen: true))
69+
// Numbers must be written with the invariant culture: under de-DE the writer would emit 1.5 as "1,5",
70+
// which the raw value below would pass through as invalid JSON.
71+
// The writer emits UTF-8 without a BOM; disposing it closes the stream, which ToArray still reads.
72+
using (var textWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture))
7173
{
7274
var openApiWriter = new OpenApiJsonWriter(textWriter);
7375
SerializeSchema(value, openApiWriter);

‎src/Microsoft.OpenApi/Models/OpenApiDocument.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#if NET
77
using System.Collections.Immutable;
88
#endif
9+
using System.Globalization;
910
using System.IO;
1011
using System.Linq;
1112
using System.Security.Cryptography;
@@ -528,7 +529,7 @@ public async Task<string> GetHashCodeAsync(CancellationToken cancellationToken =
528529
{
529530
#if NET7_OR_GREATER
530531
using var memoryStream = new MemoryStream();
531-
using var streamWriter = new StreamWriter(memoryStream);
532+
using var streamWriter = new FormattingStreamWriter(memoryStream, CultureInfo.InvariantCulture);
532533

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

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

543545
await WriteDocumentAsync(streamWriter, cancellationToken).ConfigureAwait(false);
544546
#if NET5_0_OR_GREATER

‎test/Microsoft.OpenApi.Tests/Converters/OpenApiSchemaJsonConverterTests.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Globalization;
67
using System.Text.Json;
8+
using System.Text.Json.Nodes;
79
using Xunit;
810

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

26+
[Theory]
27+
[InlineData("de-DE")] // writes 1.5 as "1,5"
28+
[InlineData("fi-FI")] // writes -5 with U+2212 MINUS SIGN
29+
public void Serialize_NumericValues_DoNotDependOnCurrentCulture(string culture)
30+
{
31+
var schema = new OpenApiSchema
32+
{
33+
Type = JsonSchemaType.Number,
34+
Default = JsonValue.Create(1.5),
35+
Enum = [JsonValue.Create(-5), JsonValue.Create(1.5)]
36+
};
37+
38+
var originalCulture = CultureInfo.CurrentCulture;
39+
string json;
40+
try
41+
{
42+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
43+
json = JsonSerializer.Serialize(schema, _optionsV31);
44+
}
45+
finally
46+
{
47+
CultureInfo.CurrentCulture = originalCulture;
48+
}
49+
50+
using var doc = JsonDocument.Parse(json);
51+
Assert.Equal(1.5, doc.RootElement.GetProperty("default").GetDouble());
52+
Assert.Equal(-5, doc.RootElement.GetProperty("enum")[0].GetInt32());
53+
}
54+
2455
[Fact]
2556
public void Serialize_SimpleStringSchema_ProducesOpenApiWireFormat()
2657
{

‎test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,39 @@ public async Task SerializeRelativeRootPathWithHostAsV2JsonWorks()
16471647
Assert.Equal(expected, actual);
16481648
}
16491649

1650+
[Fact]
1651+
public async Task HashCodeDoesNotDependOnCurrentCulture()
1652+
{
1653+
var document = new OpenApiDocument
1654+
{
1655+
Info = new OpenApiInfo { Title = "Culture", Version = "1.0" },
1656+
Components = new OpenApiComponents
1657+
{
1658+
Schemas = new Dictionary<string, IOpenApiSchema>
1659+
{
1660+
["price"] = new OpenApiSchema { Type = JsonSchemaType.Number, Default = JsonValue.Create(1.5) }
1661+
}
1662+
}
1663+
};
1664+
1665+
var originalCulture = CultureInfo.CurrentCulture;
1666+
string invariantHash, germanHash;
1667+
try
1668+
{
1669+
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
1670+
invariantHash = await document.GetHashCodeAsync(TestContext.Current.CancellationToken);
1671+
// de-DE writes 1.5 as "1,5"
1672+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
1673+
germanHash = await document.GetHashCodeAsync(TestContext.Current.CancellationToken);
1674+
}
1675+
finally
1676+
{
1677+
CultureInfo.CurrentCulture = originalCulture;
1678+
}
1679+
1680+
Assert.Equal(invariantHash, germanHash);
1681+
}
1682+
16501683
[Fact]
16511684
public async Task TestHashCodesForSimilarOpenApiDocuments()
16521685
{

0 commit comments

Comments
 (0)