Skip to content

OpenApiSchemaJsonConverter writes invalid JSON under cultures with a decimal comma #3185

Closed

Description

@menes97

Describe the bug

OpenApiSchemaJsonConverter.Write serializes the schema through a StreamWriter. That writer's FormatProvider is the current culture. The result is then passed to Utf8JsonWriter.WriteRawValue(..., skipInputValidation: true).

Numbers in the schema are therefore written with the current culture, and the invalid output is not caught:

  • under de-DE, a default of 1.5 is written as "default": 1,5;
  • under fi-FI, -5 is written with U+2212 MINUS SIGN.

Both make the serialized JSON invalid.

OpenApiDocument.GetHashCodeAsync uses the same kind of writer, so a document's hash depends on the machine's culture.

To reproduce

CultureInfo.CurrentCulture = new CultureInfo("de-DE");
var schema = new OpenApiSchema { Type = JsonSchemaType.Number, Default = JsonValue.Create(1.5) };
var json = JsonSerializer.Serialize(schema, new JsonSerializerOptions { Converters = { new OpenApiSchemaJsonConverter() } });
JsonDocument.Parse(json); // JsonReaderException: '5' is an invalid start of a property name

Expected behavior

The output is the same as under the invariant culture: "default": 1.5. This is what the SerializeAsync extensions already produce, because they use FormattingStreamWriter with CultureInfo.InvariantCulture.

I have a fix with tests ready and will open a PR that links this issue.

This issue was written with AI assistance (Claude Code); I reproduced the bug and reviewed the text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions