Fix example XML doc parsing for OpenApi#67397
Open
Youssef1313 wants to merge 1 commit into
Open
Conversation
Youssef1313
commented
Jun 24, 2026
| else if (decimal.TryParse(value, out var decimalValue)) | ||
| { | ||
| return JsonValue.Create(decimalValue); | ||
| } |
Member
Author
There was a problem hiding this comment.
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)
Contributor
There was a problem hiding this comment.
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.Parseto constructJsonNodevalues from raw strings (viaTryParse+JsonValue.Create) instead of callingJsonNode.Parseup-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)) |
Open
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Using
JsonNode.Parsein 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