Skip to content

JsonReaderExceptions in JsonNodeExtensions spoils debugging in Visual Studio #65242

@aureole82

Description

@aureole82

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
calls 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

  1. Consider the schema! If it's a string always call JsonValue.Create(exampleString). Nobody has <example>"EN"</example> XML comments!
  2. Or more flexible: If string and
    • quoted: "EN"JsonNode.Parse(json),
    • otherwise ENJsonValue.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

/// <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

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions