Feature/generator base - #205
Merged
Merged
Conversation
…y every test Each converter used to repeat a ~290-line skeleton identical across the hierarchy; only ~15 lines were specific (T, discriminator table, base properties). Each converter test covered only a slice (12-37%), dragging generated-code coverage to ~24%. The generator now emits JsonSubTypesAotConverterBase<T> (Read, dynamic-subtype machinery, base-object helpers, abstract SelectType/DeserializeBase/WriteBaseObject/ TryWriteNestedObject) and JsonSubTypesAotValueConverterBase<T> (the discriminator- injection Write, IsRegistered, WriteDiscriminatorValue, virtual DiscriminatorWriters) once per compilation. Converters become ~30-line subclasses overriding only the hierarchy-specific members. Presence-mode converters inherit the first base only. Both bases are generated (not library code): JsonSubTypes.Aot ships only as an analyzer (no runtime assembly), and generated bases keep the converter output self-contained and the golden master faithful to the generator output. Verified: 22 generator-driver tests (golden master regenerated), 73 Aot.Tests parity tests on net8.0 and net10.0.
This file carries two changes in one commit. The bulk is a refactor of GetTypeByPropertyPresence (single-pass lookup, StopLookupOnMatch return, Count > 1 ambiguity check, SingleOrDefault) authored by a parallel agent session on this worktree; it was never committed and broke the net35 build because string.Join(string, IEnumerable<string>) does not exist on .NET 3.5. The commit adds back the ToArray() the previous code had. Behavior is unchanged: same types found, same first-match shortcut, same ambiguity error. Verified by the 147 Newtonsoft tests on net8.0 and net10.0 and by the net35 project compiling clean.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #205 +/- ##
==========================================
- Coverage 90.21% 88.91% -1.31%
==========================================
Files 19 19
Lines 1789 1515 -274
Branches 294 292 -2
==========================================
- Hits 1614 1347 -267
+ Misses 106 101 -5
+ Partials 69 67 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Sonar flags on the committed golden master: the value-mode Write had cyclomatic complexity 18 and the subclass files carried double blank lines (and a blank line right after an opening brace in presence mode). The Write now delegates payload resolution to TryGetPayload (Write: 6, helper: 5) with identical behavior, verified by the 73 parity tests. The EmitConverter template inlines the value-mode members so no stray blank lines appear, and TryWriteNestedObject omits the empty line when there are no nested chains. Also verified the CodeFactor badge already exists in README.md, so nothing was added there.
Value-mode SelectType was 18 (Cognitive Complexity limit 15): it now
delegates the static discriminator table to a private ResolveStaticType
helper (SelectType 6, helper under limit) with identical behavior, verified
by the parity tests. The duplicated discriminator literal is emitted once as
a private const DiscriminatorPropertyNameValue, reused by DiscriminatorPropertyName,
SelectType and the nested chains that reference the converter's own discriminator.
Shared base cleanup: DynamicSubtypes becomes a get-only property (S3887),
the case-insensitive and discriminator-skip loops use Where (S3267),
IndexOf('.') becomes Contains('.') (CA2249), and the bases source is a const
(S3400). Generator internals stop repeating the ' {' / ' }'
fragments (S1192). The driver now references System.Linq because the emitted
bases use Where.
Tests: JsonSerializerOptions instances are cached instead of rebuilt per
operation, and the expected hints array is a static readonly field.
Sonar's S2583 on JsonSubtypes.cs (the 'Count > 1' ambiguity check) is left
untouched: it is a false positive - the throw is exercised by
DemoKnownSubTypeWithPropertyTests and DemoKnownSubTypeWithMultiplePropertiesTests
- and the concurrent refactor is behaviorally identical to the previous code.
Verified: 22 generator tests, 73 Aot parity tests, 190 Text.Json tests,
147 Newtonsoft tests (net8 + net10), solution build, sample build.
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.
Description
Checklist
Please make sure the following points are addressed before the PR is reviewed:
[Unreleased]section ofCHANGELOG.mddescribing the change (bug fix, feature, breaking change), with the related issue number when applicable.