Skip to content

Commit

Permalink
MartenOps.StartStream() can guard against empty or null stream keys w…
Browse files Browse the repository at this point in the history
…hen using string identified streams. Closes GH-1269
  • Loading branch information
jeremydmiller committed Feb 18, 2025
1 parent 6bd7d41 commit 76539a6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using IntegrationTests;
using Marten;
using Marten.Events;
using MartenTests.Distribution.TripDomain;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Oakton.Resources;
using Shouldly;
using Wolverine;
using Wolverine.Marten;

namespace MartenTests;

public class validate_empty_stream_key_on_start_stream: PostgresqlContext, IAsyncLifetime
{
private IHost _host;
private IDocumentStore _store;

public async Task InitializeAsync()
{
_host = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
opts.Services
.AddMarten(m =>
{
m.Connection(Servers.PostgresConnectionString);
m.DatabaseSchemaName = "start_stream";
m.Events.StreamIdentity = StreamIdentity.AsString;
})
.IntegrateWithWolverine();

opts.Policies.AutoApplyTransactions();

opts.Services.AddResourceSetupOnStartup();
}).StartAsync();

_store = _host.Services.GetRequiredService<IDocumentStore>();

await _store.Advanced.Clean.DeleteDocumentsByTypeAsync(typeof(NamedDocument));
}

public async Task DisposeAsync()
{
await _host.StopAsync();
_host.Dispose();
}

[Fact]
public void assert_empty_stream_key()
{
using var session = _store.LightweightSession();

// No stream key supplied
var op = MartenOps.StartStream<Trip>(new TripStarted());

Should.Throw<InvalidOperationException>(() => op.Execute(session));
}

[Fact]
public void happy_path_execution()
{
using var session = _store.LightweightSession();

// No stream key supplied
var op = MartenOps.StartStream<Trip>("a good key", new TripStarted());
op.Execute(session);
}
}
9 changes: 9 additions & 0 deletions src/Persistence/Wolverine.Marten/IMartenOp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using JasperFx.CodeGeneration.Model;
using JasperFx.Core;
using Marten;
using Marten.Events;
using Marten.Internal.Sessions;
using Wolverine.Configuration;
using Wolverine.Marten.Persistence.Sagas;
using Wolverine.Runtime;
Expand Down Expand Up @@ -358,6 +360,13 @@ public StartStream<T> With(object @event)

public void Execute(IDocumentSession session)
{
if (session is DocumentSessionBase s && s.Options.Events.StreamIdentity == StreamIdentity.AsString &&
StreamKey.IsEmpty())
{
throw new InvalidOperationException(
"The event stream identity is string, but the StreamKey is empty or null");
}

if (StreamId == Guid.Empty)
{
if (StreamKey.IsNotEmpty())
Expand Down

0 comments on commit 76539a6

Please sign in to comment.