Skip to content

Feature/generator base - #205

Merged
manuc66 merged 4 commits into
masterfrom
feature/generator-base
Aug 13, 2026
Merged

Feature/generator base#205
manuc66 merged 4 commits into
masterfrom
feature/generator-base

Conversation

@manuc66

@manuc66 manuc66 commented Aug 13, 2026

Copy link
Copy Markdown
Owner

Description

Checklist

Please make sure the following points are addressed before the PR is reviewed:

  • Changelog: an entry was added to the [Unreleased] section of CHANGELOG.md describing the change (bug fix, feature, breaking change), with the related issue number when applicable.
  • Tests: the change is covered by tests, and the test suite passes.
  • Documentation: README or XML docs were updated when the public API or behavior changed.

…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

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.96386% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.91%. Comparing base (c577fb4) to head (849ee7a).

Files with missing lines Patch % Lines
JsonSubTypes.Aot/JsonSubTypesGenerator.cs 90.38% 11 Missing and 4 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.
@manuc66
manuc66 merged commit 4d50e4c into master Aug 13, 2026
9 checks passed
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.

1 participant