Skip to content

OpenApiJsonWriter produces non-standard JSON values when a schema has 'NaN' or 'Infinity' values #1519

@peombwa

Description

@peombwa

Describe the bug
OpenApiJsonWriter produces non-standard JSON values when an OpenAPI schema has NaN or +/-Infinity values.

JSON specification does not support unquoted NaN or Infinity values. While some JSON parsers (like JavaScript's JSON.parse) can handle these values, others (like Python's json module or .NET's System.Text.Json) cannot. This can lead to interoperability issues when different systems interact with the JSON output of 'OpenApiJsonWriter'.

OpenApi File To Reproduce

var schema = new OpenApiSchema
{
  Enum = new List<Microsoft.OpenApi.Any.IOpenApiAny> {
    new OpenApiDouble(double.NaN),
    new OpenApiDouble(double.PositiveInfinity),
    new OpenApiDouble(double.NegativeInfinity) }
};

var schemaBuilder = new StringBuilder();
var jsonWriter = new OpenApiJsonWriter(new StringWriter(schemaBuilder));
schema.SerializeAsV3(jsonWriter);
var jsonString = schemaBuilder.ToString(); // <--- The resulting JSON string is not a valid JSON.

var jsonElement = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonString); // <--- Throws " 'N' is an invalid start of a value".

Expected behavior
The resulting NaN or Infinity values should have quotes around them to be considered valid JSON. Adding quotes around the values will ensure that the resulting JSON OpenAPI document is fully compliant with the JSON specification and can be correctly processed by any JSON parser.

Screenshots/Code Snippets
N/A

Additional context
N/A

Metadata

Metadata

Assignees

Labels

type:bugA broken experience

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions