Skip to content

Fix NaturalKeySource discovery when methods are on the projection class#4240

Merged
jeremydmiller merged 1 commit into
masterfrom
fix/natural-key-source-on-projection
Apr 7, 2026
Merged

Fix NaturalKeySource discovery when methods are on the projection class#4240
jeremydmiller merged 1 commit into
masterfrom
fix/natural-key-source-on-projection

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Summary

When [NaturalKeySource] methods are defined on the projection class (e.g., SingleStreamProjection<TDoc, TId>) rather than the aggregate type itself, the natural key discovery failed to find them. This caused FetchLatest and FetchForWriting with natural keys to return null.

Root Cause

discoverNaturalKey() only searched the aggregate type (typeof(TDoc)) for [NaturalKeySource] methods. When Create/Apply methods with the attribute were on the projection class, the EventMappings list remained empty and the NaturalKeyProjection never populated the lookup table.

Fix (in JasperFx.Events)

  1. discoverNaturalKey() now also searches GetType() (the projection class) for [NaturalKeySource] methods
  2. For aggregate instance methods: preserve the original working pattern (create instance, call method, read property)
  3. For projection static methods: find a matching property on the event data type by natural key type (since calling the static method may crash when it needs IEvent.StreamKey)

Test

New test natural_key_source_on_projection_class_with_string_streams with BookListing aggregate + Isbn natural key where all [NaturalKeySource] methods are on the projection class.

Depends on JasperFx/jasperfx@b5aba64 (pushed to main)

Test plan

  • 28/28 natural key tests passing (27 existing + 1 new), zero regressions

🤖 Generated with Claude Code

When [NaturalKeySource] methods are on the projection class (e.g.,
SingleStreamProjection<TDoc, TId>) rather than the aggregate, natural
key discovery now finds them. Previously FetchLatest/FetchForWriting
with natural keys returned null in this case.

- Upgrade JasperFx.Events to 1.26.1 (contains the discovery fix)
- Bump Marten to 8.29.1
- New test: BookListing aggregate with Isbn natural key where all
  [NaturalKeySource] methods are on the projection class
- 29/29 natural key tests passing, zero regressions

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jeremydmiller jeremydmiller force-pushed the fix/natural-key-source-on-projection branch from cc84c80 to f9a80fc Compare April 7, 2026 17:21
@jeremydmiller jeremydmiller merged commit 82373f8 into master Apr 7, 2026
6 checks passed
@jeremydmiller jeremydmiller deleted the fix/natural-key-source-on-projection branch April 7, 2026 17:28
This was referenced May 11, 2026
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