diff --git a/Directory.Packages.props b/Directory.Packages.props index 9f90370d58..d8cf98790e 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -14,7 +14,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/EventSourcingTests/Bugs/Bug_4197_fetch_for_writing_natural_key.cs b/src/EventSourcingTests/Bugs/Bug_4197_fetch_for_writing_natural_key.cs index 1d18149a40..d1f060773c 100644 --- a/src/EventSourcingTests/Bugs/Bug_4197_fetch_for_writing_natural_key.cs +++ b/src/EventSourcingTests/Bugs/Bug_4197_fetch_for_writing_natural_key.cs @@ -92,4 +92,39 @@ public async Task fetch_for_writing_with_natural_key_with_inline_snapshot() stream.Aggregate.ShouldNotBeNull(); stream.Aggregate.Key.ShouldBe(aggregateKey); } + + // Regression coverage for https://github.com/JasperFx/marten/issues/4277. + // Self-aggregating aggregate (record) with a static [NaturalKeySource] factory. + public sealed record Bug4277SelfAggregate(Guid Id, [property: NaturalKey] Bug4197AggregateKey Key) + { + [NaturalKeySource] + public static Bug4277SelfAggregate Create(Bug4197AggregateCreatedEvent e) + => new(e.Id, new Bug4197AggregateKey(e.Key)); + } + + [Fact] + public async Task fetch_for_writing_with_natural_key_on_self_aggregating_record_with_static_source() + { + StoreOptions(opts => + { + opts.Projections.Snapshot(SnapshotLifecycle.Inline); + }); + + await using var session = theStore.LightweightSession(); + + var aggregateId = Guid.NewGuid(); + var aggregateKey = new Bug4197AggregateKey("another-key-value"); + var e = new Bug4197AggregateCreatedEvent(aggregateId, aggregateKey.Value); + + session.Events.StartStream(aggregateId, e); + await session.SaveChangesAsync(); + + // Before the fix, the natural-key lookup row was never written, so this + // FetchForWriting by natural key returned a null aggregate. + var stream = await session.Events.FetchForWriting(aggregateKey); + + stream.ShouldNotBeNull(); + stream.Aggregate.ShouldNotBeNull(); + stream.Aggregate.Key.ShouldBe(aggregateKey); + } }