Fix #4680 — upcaster shadowed when source type is typed-appended in same DocumentStore#4694
Merged
jeremydmiller merged 1 commit intoJun 8, 2026
Conversation
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>
This was referenced Jun 10, 2026
Merged
This was referenced Jun 17, 2026
This was referenced Jun 19, 2026
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.
Closes #4680.
When
Upcast<TOld, TNew>(...)is registered AND aTOldinstance is appended (typed) into the sameDocumentStore, reading back returnedTOldinstead ofTNew. 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
Upcast(typeof(TNew), oldEventName, transformation)builds anEventMapping<TNew>keyed underoldEventNamein_byEventName. ItsReadEventDataruns the JsonTransformation to produce TNew.Append(TOld)flows throughEventMappingFor(typeof(TOld)), whose OnMissing factory lazily creates anEventMapping<TOld>. The new mapping lives in the registry alongside the upcaster mapping.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 theEventMapping<TOld>the typed Append created, replaces the upcaster, and the deserializer materialisesTOld-- 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.IsUpcastTargetflag, set inEventGraph.Upcast(...).EventDocumentStorage.Resolve/ResolveAsyncskip 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.Including the 2 new bug tests; no other regressions.
🤖 Generated with Claude Code