Skip to content

fix(csharp): throw JsonException/NotSupportedException in SystemTextJson converters#2976

Merged
schani merged 1 commit into
masterfrom
agent/fix-issue-2364
Jul 20, 2026
Merged

fix(csharp): throw JsonException/NotSupportedException in SystemTextJson converters#2976
schani merged 1 commit into
masterfrom
agent/fix-issue-2364

Conversation

@schani

@schani schani commented Jul 20, 2026

Copy link
Copy Markdown
Member

Bug

Per MS docs (https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-7-0#error-handling), custom System.Text.Json JsonConverters should throw JsonException (malformed/unexpected tokens) or NotSupportedException, so the serializer can attach JSON path/line/position diagnostics to the error. Generated C# SystemTextJson union converters instead threw a bare System.Exception, losing that diagnostic info for callers.

Root cause

Both throw sites in emitTransformation (the union-type read/write converter emitter) funneled through the emitThrow() helper in packages/quicktype-core/src/language/CSharp/SystemTextJsonCSharpRenderer.ts, which always emitted throw new Exception(...).

Fix

emitThrow() now takes an explicit exception type parameter:

  • The Read (unmarshal) failure path now throws JsonException.
  • The Write (marshal) failure path now throws NotSupportedException.

This only affects the SystemTextJson C# framework (--framework SystemTextJson); the NewtonSoft C# renderer is untouched.

Repro

node dist/index.js --lang csharp --src-lang json --framework SystemTextJson -o out.cs sample.json

with sample.json:

[
  {"kind": "alpha", "mixed": 5},
  {"kind": "beta", "mixed": "five"}
]

Before: throw new Exception("Cannot unmarshal type Mixed"); / throw new Exception("Cannot marshal type Mixed");
After: throw new JsonException("Cannot unmarshal type Mixed"); / throw new NotSupportedException("Cannot marshal type Mixed");

Test coverage

Since the existing end-to-end fixtures only check that the driver process exits nonzero on failure (any exception type satisfies that — they can't distinguish Exception from JsonException), this change is covered by a new unit test, test/unit/csharp-system-text-json-exceptions.test.ts, following the established "assert on generated source text" pattern (see test/unit/swift-final-classes.test.ts). It renders a schema producing a C# union with framework: SystemTextJson and asserts the generated source contains JsonException/NotSupportedException throws and no bare throw new Exception(.

Verification

  • Confirmed the regression test fails against the pre-fix code.
  • npm run build passes.
  • npm run test:unit — all 164 tests pass.
  • Manually re-ran the CLI repro above and confirmed the generated code now throws JsonException/NotSupportedException.
  • The dotnet toolchain is not available in this environment, so the csharp-SystemTextJson end-to-end fixture (FIXTURE=csharp-SystemTextJson script/test) was not run locally; CI will validate that the generated code still compiles and the union round-trip fixtures still pass (they only assert nonzero exit on failure, so this change is expected to be compatible).

Fixes #2364

🤖 Generated with Claude Code

…son converters (#2364)

Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
@schani
schani merged commit 1886c1c into master Jul 20, 2026
28 checks passed
@schani
schani deleted the agent/fix-issue-2364 branch July 20, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: C# Use the right exception for System.Text.Json converters

1 participant