Skip to content

Fix #4680 — upcaster shadowed when source type is typed-appended in same DocumentStore#4694

Merged
jeremydmiller merged 1 commit into
masterfrom
feature/4680-upcaster-shadowed-by-typed-append
Jun 8, 2026
Merged

Fix #4680 — upcaster shadowed when source type is typed-appended in same DocumentStore#4694
jeremydmiller merged 1 commit into
masterfrom
feature/4680-upcaster-shadowed-by-typed-append

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Closes #4680.

When Upcast<TOld, TNew>(...) is registered AND a TOld instance is appended (typed) into the same DocumentStore, reading back returned TOld instead of TNew. Reading from a different store instance worked, which the issue reporter narrowed to "appending the typed source event registers a concrete EventMapping for TOld that shadows the upcaster on read."

Mechanism

Step What happens
1 Upcast(typeof(TNew), oldEventName, transformation) builds an EventMapping<TNew> keyed under oldEventName in _byEventName. Its ReadEventData runs the JsonTransformation to produce TNew.
2 Typed Append(TOld) flows through EventMappingFor(typeof(TOld)), whose OnMissing factory lazily creates an EventMapping<TOld>. The new mapping lives in the registry alongside the upcaster mapping.
3 On EventDocumentStorage.Resolve / ResolveAsync, the by-name lookup returns the upcaster (good), but then the dotnet_type column fires the existing alt-mapping swap: TryGetRegisteredMappingForDotNetTypeName(dotnetTypeName) finds the EventMapping<TOld> the typed Append created, replaces the upcaster, and the deserializer materialises TOld -- bypassing the upcaster entirely.

The alt-mapping swap is correct for its original purpose: same EventTypeName, multiple CLR types, NO upcaster registered (e.g. moved-namespace renames). The bug is that the swap fires unconditionally even when the by-name mapping is an authoritative upcaster.

Fix

New EventMapping.IsUpcastTarget flag, set in EventGraph.Upcast(...). EventDocumentStorage.Resolve / ResolveAsync skip the dotnet_type alt-mapping swap when the by-name mapping is an upcast target. The swap remains for the non-upcaster case it was originally added for.

Tests

src/EventSourcingTests/SchemaChange/Bug_4680_upcaster_shadowed_by_typed_append.cs:

  • same_store_typed_append_is_upcast_on_read -- the headline repro. Failing before this commit, passing after.
  • alt_mapping_swap_still_works_when_no_upcaster_registered -- regression pin for the alt-mapping swap's legitimate use case. Two CLR types share \"shared_event_name\" with no upcaster; the dotnet_type column should still steer the resolver to the appended type's mapping. Pinning this protects against the fix over-rotating.
Project Result
EventSourcingTests 1363 / 1370 (7 unrelated skips) on net9.0

Including the 2 new bug tests; no other regressions.

🤖 Generated with Claude Code

When Upcast<TOld, TNew>(...) was registered AND a TOld instance was appended
(typed) into the same DocumentStore, reading back returned TOld instead of
TNew. Loading from a different store instance (one that registered the
upcaster but never appended TOld) read correctly, which the issue reporter
already narrowed down to "appending the typed source event registers a
concrete EventMapping for TOld that shadows the upcaster on read."

## Mechanism

1. `EventGraph.Upcast(typeof(TNew), oldEventName, transformation)` builds an
   `EventMapping<TNew>` keyed under `oldEventName` in `_byEventName`. Its
   `ReadEventData` runs the JsonTransformation to produce TNew.
2. Typed `Append(TOld)` flows through `EventMappingFor(typeof(TOld))`, whose
   OnMissing factory lazily creates an `EventMapping<TOld>` (and calls
   `Storage.AddMapping`). The new mapping lives in the registry alongside the
   upcaster mapping.
3. On `EventDocumentStorage.Resolve` / `ResolveAsync`:
   * The by-name lookup returns the upcaster mapping (good).
   * The dotnet_type column read then fires the existing alt-mapping swap:
     `TryGetRegisteredMappingForDotNetTypeName(dotnetTypeName)` finds the
     `EventMapping<TOld>` the typed Append created, replaces the upcaster
     with it, and the deserializer materialises TOld -- bypassing the
     upcaster entirely.

The alt-mapping swap is correct for its original purpose: same eventTypeName,
multiple CLR types, NO upcaster registered (e.g. moved-namespace renames).
The bug is that the swap fires unconditionally even when the by-name mapping
is an authoritative upcaster.

## Fix

New `EventMapping.IsUpcastTarget` flag, set in `EventGraph.Upcast(...)`.
`EventDocumentStorage.Resolve` / `ResolveAsync` skip the dotnet_type
alt-mapping swap when the by-name mapping is an upcast target. The swap
remains for the non-upcaster case it was originally added for.

## Tests

`src/EventSourcingTests/SchemaChange/Bug_4680_upcaster_shadowed_by_typed_append.cs`:

* `same_store_typed_append_is_upcast_on_read` — the headline repro. Failing
  before this commit, passing after.
* `alt_mapping_swap_still_works_when_no_upcaster_registered` — regression
  pin for the alt-mapping swap's legitimate use case. Two CLR types share
  `"shared_event_name"` with no upcaster; the dotnet_type column should still
  steer the resolver to the appended type's mapping. Pinning this protects
  against the fix over-rotating.

Full EventSourcingTests passes (1363/1370, 7 unrelated skips) on net9.0.

Closes #4680.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jeremydmiller jeremydmiller merged commit ad68a7f into master Jun 8, 2026
8 checks passed
@jeremydmiller jeremydmiller deleted the feature/4680-upcaster-shadowed-by-typed-append branch June 8, 2026 19:30
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.

Upcaster bypassed when the source event type is appended (typed) into the same DocumentStore

1 participant