OpenAPITools / OpenAPITools/openapi-generator
[BUG][csharp][generichost] Wrong error type used during deserialization
Nobody has claimed this yet.
- 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:
-
System.Text.Jsonpath enrichment is bypassed. When aJsonExceptionis thrown from inside a converter, the serializer automatically populates the exception'sPathproperty 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. -
Callers catch the wrong type. Code that wraps
JsonSerializer.Deserialize<T>()in acatch (JsonException)block will not catchArgumentExceptionorArgumentNullException. This causes unexpected unhandled exceptions in calling code that correctly handlesJsonException.
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
generichostC# generator that have required or non-nullable properties. ArgumentNullExceptionandArgumentExceptionare appropriate for public API argument validation (constructors, methods), not for JSON deserialization failures.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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