feat(csharp): option to suppress DateOnly/TimeOnly converters (System.Text.Json)#2962
Merged
Conversation
….Text.Json) The System.Text.Json C# renderer unconditionally emits DateOnlyConverter and TimeOnlyConverter helper classes and registers them in Converter.Settings. DateOnly and TimeOnly only exist on .NET 6+, so the generated code does not compile on .NET Standard or older targets (issue #2629). Add a System.Text.Json-only boolean option, --[no-]dateonly-timeonly-converters (on by default), that suppresses both the converter classes and their registration. Nothing else in the generated code references them, so suppressed output still compiles and round-trips; default output is byte-for-byte identical. The option lives in systemTextJsonCSharpOptions rather than the shared cSharpOptions, so only the System.Text.Json renderer's typed options include it; CSharpTargetLanguage.getOptions() now returns the System.Text.Json superset so the CLI accepts the flag. Covered by a pinned csharp-SystemTextJson quick-test (unions.json with the converters suppressed must compile and round-trip) and a unit test asserting the converter classes' presence by default and absence under the flag. Reimplementation of #2842 by youcefnb (findyoucef); supersedes it. Fixes #2629. Co-authored-by: youcefnb <youcef@colorado.edu> Co-Authored-By: Claude Fable 5 <noreply@anthropic.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.
Supersedes #2842, fixes #2629.
This is a reimplementation of @findyoucef's PR #2842, which could not be rebased (conflicts with the C# 8 default work plus unrelated dependency churn). The core idea — making the System.Text.Json
DateOnly/TimeOnlyconverter emission optional — is theirs; their authorship is preserved via aCo-authored-bytrailer on the commit.Problem
The System.Text.Json C# renderer unconditionally emits
DateOnlyConverterandTimeOnlyConverterhelper classes and registers them inConverter.Settings.DateOnly/TimeOnlyonly exist on .NET 6+, so the generated code does not compile on .NET Standard or older target frameworks (#2629).Change
A new System.Text.Json-only boolean option:
With
--no-dateonly-timeonly-converters, both converter classes and theirConverters = { ... }registrations are omitted. Nothing else in the generated code references them, so the output still compiles and round-trips. Default output is byte-for-byte identical to master.The option lives in
systemTextJsonCSharpOptionsrather than the sharedcSharpOptions, so only the System.Text.Json renderer's typed options include it — it is not a silent no-op field on the Newtonsoft options object (the original PR put its flags in the shared options).CSharpTargetLanguage.getOptions()now returns the System.Text.Json superset so the CLI accepts and documents the flag.Kept from #2842 vs. dropped
no-dateonly/no-timeonly), sinceDateOnlyandTimeOnlywere introduced together in .NET 6 and no target wants only one of them.csTypeLocalDateOnly→DateTime / TimeOnly→TimeSpan remapping helper (dead code — the C# type mapper never producesDateOnly/TimeOnlyproperty types; dates map toDateTimeOffset); thepackage.json/package-lock.jsondependency bumps;.vscode/tasks.json; and the whitespace reformat of the converter boilerplate (keeping default output byte-identical).Testing
csharp-SystemTextJsongains a pinned quick-test —unions.jsonwithdateonly-timeonly-converters: false— proving that generated code with the converters suppressed still compiles and round-trips.test/unit/csharp-dateonly-timeonly-converters.test.ts: default output contains both converter classes and registrations; with the flag set it contains neither, whileIsoDateTimeOffsetConverteris unaffected.FIXTURE=csharp-SystemTextJson(236/236 samples),FIXTURE=schema-csharp-SystemTextJson,FIXTURE=csharp,FIXTURE=schema-csharp, andnpm run test:unit(165 tests, type tests included).🤖 Generated with Claude Code