Skip to content

fix: write schema JSON and document hashes with the invariant culture - #3186

Merged
Vincent Biret (baywet) merged 1 commit into
microsoft:mainfrom
menes97:fix/schema-converter-invariant-culture
Nov 23, 2026
Merged

Vincent Biret (baywet) merged 1 commit into
microsoft:mainfrom
menes97:fix/schema-converter-invariant-culture

Conversation

@menes97

@menes97 menes97 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Pull Request

Description

OpenApiSchemaJsonConverter.Write wrote the schema through a StreamWriter. That writer formats numbers with the current culture, and the result was then passed to WriteRawValue without validation:

  • under de-DE, 1.5 became 1,5;
  • under fi-FI, -5 was written with U+2212.

Both produce invalid JSON. OpenApiDocument.GetHashCodeAsync had the same writer, so its hash depended on the machine's culture.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Related Issue(s)

Fixes #3185

Changes Made

  • OpenApiSchemaJsonConverter.Write writes through FormattingStreamWriter(stream, CultureInfo.InvariantCulture), as the SerializeAsync extensions already do. That writer also emits UTF-8 without a BOM. MemoryStream.ToArray still works after the writer closes the stream, so the custom encoding field was removed.
  • OpenApiDocument.GetHashCodeAsync uses the same writer in both of its branches.
  • The new tests:
    • Serialize_NumericValues_DoNotDependOnCurrentCulture (de-DE, fi-FI) serializes a schema with a default of 1.5 and an enum value of -5 and parses the result back;
    • HashCodeDoesNotDependOnCurrentCulture compares the hash under the invariant culture with the hash under de-DE.

Testing

  • Unit tests added/updated
  • All existing tests pass

All three new test cases fail without the fix:

  • de-DE: '5' is an invalid start of a property name;
  • fi-FI: '0xE2' is an invalid start of a value;
  • the two hashes differ.

With the fix:

  • Microsoft.OpenApi.Tests: 1335/1335 pass;
  • Microsoft.OpenApi.Readers.Tests: 710/710 pass;
  • the library builds for every target framework, including netstandard2.0.

Checklist

  • My code follows the code style of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Versions applicability

Additional Notes

This PR was written with AI assistance (Claude Code). I reproduced the bug, ran the tests and reviewed the change.

🤖 Generated with Claude Code

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>
@menes97
menes97 requested a review from a team as a code owner November 22, 2026 08:22
Copilot AI balanced review requested due to automatic review settings November 22, 2026 08:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues blocking the culture-independent schema serialization and document-hashing changes.

No open issues 🎉

What changed in this PR

Fixes #3185 by making schema JSON serialization and document hashing culture-independent, aligning both paths with the library’s existing invariant-culture serialization.

Changes:

  • Uses FormattingStreamWriter with invariant culture for schema serialization and both document-hashing branches.
  • Removes the converter’s redundant UTF-8 encoding field while retaining BOM-free output.
  • Adds regression tests for numeric JSON under de-DE and fi-FI, and hash consistency across cultures.
File Description
test/​Microsoft.OpenApi.Tests/​Models/​OpenApiDocumentTests.cs Tests hash equality under invariant and German cultures.
test/​Microsoft.OpenApi.Tests/​Converters/​OpenApiSchemaJsonConverterTests.cs Tests valid numeric JSON under German and Finnish cultures.
src/​Microsoft.OpenApi/​Models/​OpenApiDocument.cs Uses invariant-culture formatting in both hashing branches.
src/​Microsoft.OpenApi/​Converters/​OpenApiSchemaJsonConverter.cs Uses invariant-culture formatting and removes the redundant encoding field.

Review effort: Balanced

@menes97

menes97 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@baywet
Vincent Biret (baywet) merged commit cd7cb1c into microsoft:main Nov 23, 2026
7 checks passed
Vincent Biret (baywet) added a commit that referenced this pull request Nov 23, 2026
…#3186) (#3188)

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: menes97 <38889017+menes97@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants