dotnet / dotnet/aspnetcore

JsonReaderExceptions in JsonNodeExtensions spoils debugging in Visual Studio

Open
#65,242 2 comments 2 reactions 2 assignees Claimed by @Youssef1313 View on GitHub
area-minimal feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [x] 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: https://github.com/dotnet/aspnetcore/blob/08cadfee9e8275ae26a36df644b1f4e716c203cf/src/OpenApi/gen/XmlCommentGenerator.Emitter.cs#L447 calls `JsonNode.Parse(json)` blindly, what throws `JsonReaderException` if you have xml comments like `EN`. 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
1. Consider the schema! If it's a `string` always call `JsonValue.Create(exampleString)`. Nobody has `"EN"` XML comments!
2. Or more flexible: If `string` and
* quoted: `"EN"` → `JsonNode.Parse(json)`,
* otherwise `EN` → `JsonValue.Create(exampleString)`.
3. Maybe you could ask the JSON guys for `JsonNode.TryParse()`?
4. Move your `JsonNodeExtensions` into 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

```cs
/// Language of the product.
/// EN
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.