Skip to content

Fix example XML doc parsing for OpenApi#67397

Open
Youssef1313 wants to merge 1 commit into
mainfrom
dev/ygerges/json-parse
Open

Fix example XML doc parsing for OpenApi#67397
Youssef1313 wants to merge 1 commit into
mainfrom
dev/ygerges/json-parse

Conversation

@Youssef1313

Copy link
Copy Markdown
Member

Using JsonNode.Parse in this code path is conceptually wrong IMO, putting aside the try/catch handling.

These are code paths where we don't have a json in hand in the first place. We have a string value and want to create a json node out of it. So JsonValue.Create is the right thing to use.

Fixes #65242

Copilot AI review requested due to automatic review settings June 24, 2026 11:46
@Youssef1313 Youssef1313 requested a review from a team as a code owner June 24, 2026 11:46
else if (decimal.TryParse(value, out var decimalValue))
{
return JsonValue.Create(decimalValue);
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TODO to address before merging: add paths for DateTime and DateTimeOffset.

I need to double check the original behavior related to parsing DateTime/DateTimeOffset (i.e, what IFormatProvider / culture is used)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts how XML <example> values are converted into System.Text.Json.Nodes.JsonNode during OpenAPI generation, aiming to avoid JsonReaderException noise when examples are plain strings (e.g., EN) rather than JSON.

Changes:

  • Reworks JsonNodeExtensions.Parse to construct JsonNode values from raw strings (via TryParse + JsonValue.Create) instead of calling JsonNode.Parse up-front.
  • Expands primitive handling to cover more scalar types.

Comment on lines +587 to 591
public static JsonNode? Parse(this string? value)
{
if (json is null)
if (value is null)
{
return null;
Comment on lines +598 to +602
else if (long.TryParse(value, out var longValue))
{
try
{
// If parsing fails, try wrapping in quotes to make it a valid JSON string
return JsonNode.Parse($"\"{json.Replace("\"", "\\\"")}\"");
}
catch (JsonException)
{
return null;
}
return JsonValue.Create(longValue);
}
else if (sbyte.TryParse(value, out var sbyteValue))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonReaderExceptions in JsonNodeExtensions spoils debugging in Visual Studio

2 participants