OpenAPITools / OpenAPITools/openapi-generator

[BUG][csharp][generichost] Wrong error type used during deserialization

Open Beginner friendly
#24,345 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Post-deserialization validation throws ArgumentNullException/ArgumentException instead of JsonException

Description

After the JSON read loop in generated JsonConverter<T>.Read() implementations, the code validates required and non-nullable properties. However, it uses ArgumentException and ArgumentNullException instead of JsonException, which violates the System.Text.Json converter contract.

// Generated post-loop validation
if (!enumStringRequired.IsSet)
    throw new ArgumentException("Property is required for class EnumTest.", nameof(enumStringRequired));

if (enumStringRequired.IsSet && enumStringRequired.Value == null)
    throw new ArgumentNullException(nameof(enumStringRequired), "Property is not nullable for class EnumTest.");

Why this is wrong

JsonConverter<T>.Read() is expected to throw JsonException on invalid input. Throwing other exception types has two negative consequences:

  1. System.Text.Json path enrichment is bypassed. When a JsonException is thrown from inside a converter, the serializer automatically populates the exception's Path property with the JSON path where the error occurred (e.g. $.enum_string_required). This makes errors significantly easier to diagnose. Other exception types propagate without this context.

  2. Callers catch the wrong type. Code that wraps JsonSerializer.Deserialize<T>() in a catch (JsonException) block will not catch ArgumentException or ArgumentNullException. This causes unexpected unhandled exceptions in calling code that correctly handles JsonException.

Expected behavior

All validation errors that originate from malformed or missing JSON data should throw JsonException:

if (!enumStringRequired.IsSet)
    throw new JsonException("Property is required for class EnumTest: enum_string_required.");

if (enumStringRequired.IsSet && enumStringRequired.Value == null)
    throw new JsonException("Property is not nullable for class EnumTest: enum_string_required.");

Template location

modules/openapi-generator/src/main/resources/csharp/libraries/generichost/JsonConverter.mustache

The relevant sections are the post-loop {{#required}} and {{^isNullable}} validation blocks.

Notes

  • This affects all models generated by the generichost C# generator that have required or non-nullable properties.
  • ArgumentNullException and ArgumentException are appropriate for public API argument validation (constructors, methods), not for JSON deserialization failures.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/csharp/libraries/generichost/JsonConverter.mustache and inspect the post-loop {{#required}} and {{^isNullable}} validation blocks. Update the generated validation behavior so malformed or missing JSON data uses JsonException with the property context shown in the issue. Done means generated generichost converters no longer use ArgumentException or ArgumentNullException for these validation failures.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
82/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.