fix(csharp): throw JsonException/NotSupportedException in SystemTextJson converters#2976
Merged
Conversation
…son converters (#2364) Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.JsonJsonConverters should throwJsonException(malformed/unexpected tokens) orNotSupportedException, so the serializer can attach JSON path/line/position diagnostics to the error. Generated C#SystemTextJsonunion converters instead threw a bareSystem.Exception, losing that diagnostic info for callers.Root cause
Both throw sites in
emitTransformation(the union-type read/write converter emitter) funneled through theemitThrow()helper inpackages/quicktype-core/src/language/CSharp/SystemTextJsonCSharpRenderer.ts, which always emittedthrow new Exception(...).Fix
emitThrow()now takes an explicit exception type parameter:Read(unmarshal) failure path now throwsJsonException.Write(marshal) failure path now throwsNotSupportedException.This only affects the
SystemTextJsonC# framework (--framework SystemTextJson); theNewtonSoftC# renderer is untouched.Repro
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
ExceptionfromJsonException), 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 (seetest/unit/swift-final-classes.test.ts). It renders a schema producing a C# union withframework: SystemTextJsonand asserts the generated source containsJsonException/NotSupportedExceptionthrows and no barethrow new Exception(.Verification
npm run buildpasses.npm run test:unit— all 164 tests pass.JsonException/NotSupportedException.dotnettoolchain is not available in this environment, so thecsharp-SystemTextJsonend-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