-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Open
Labels
area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
We introduced OpenAPI document generation with Microsoft.AspNetCore.OpenApi (and replaced Swashbuckle).
But now we get tons of
System.Text.Json.JsonReaderException: ''E' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.'
The problem:
| file static class JsonNodeExtensions |
JsonNode.Parse(json) blindly, what throws JsonReaderException if you have xml comments like <example>EN</example>. Even if it's caught it spoils debugging in Visual Studio and spams the logging.
Expected Behavior
Stick to your own guidelines!!!
https://learn.microsoft.com/dotnet/standard/design-guidelines/exception-throwing
❌ DO NOT use exceptions for the normal flow of control, if possible.
I would suggest
- Consider the schema! If it's a
stringalways callJsonValue.Create(exampleString). Nobody has<example>"EN"</example>XML comments! - Or more flexible: If
stringand- quoted:
"EN"→JsonNode.Parse(json), - otherwise
EN→JsonValue.Create(exampleString).
- quoted:
- Maybe you could ask the JSON guys for
JsonNode.TryParse()? - Move your
JsonNodeExtensionsinto your code.
(Sweeping it under your rug is never good, but it would at least help me.)
Steps To Reproduce
Add a property to your API like
/// <summary> Language of the product. </summary>
/// <example>EN</example>
public required string Language { get; }and run the API in Visual Studio with all exception settings active.
Exceptions (if any)
System.Text.Json.JsonReaderException: ''E' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.'
.NET Version
10.0.102
Anything else?
No response
Metadata
Metadata
Assignees
Labels
area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi