Skip to content

Commit 9985343

Browse files
committed
move ThrowIfUnmappedEventTypes to its own subtype of topologyoptions so that it doesn't interefere with other topology types
1 parent 1525dec commit 9985343

8 files changed

+55
-38
lines changed

src/Tests/ApprovalFiles/APIApprovals.Approve.approved.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ namespace NServiceBus
5454
{
5555
protected TopicTopology(NServiceBus.Transport.AzureServiceBus.TopologyOptions options, Microsoft.Extensions.Options.IValidateOptions<NServiceBus.Transport.AzureServiceBus.TopologyOptions> optionsValidator) { }
5656
public Microsoft.Extensions.Options.IValidateOptions<NServiceBus.Transport.AzureServiceBus.TopologyOptions> OptionsValidator { get; set; }
57-
public bool ThrowIfUnmappedEventTypes { get; set; }
5857
public static NServiceBus.Transport.AzureServiceBus.TopicPerEventTopology Default { get; }
5958
protected abstract string GetPublishDestinationCore(string eventTypeFullName);
6059
public void Validate() { }
@@ -173,8 +172,13 @@ namespace NServiceBus.Transport.AzureServiceBus
173172
public void SubscribeTo<TEventType, TEventTypeImplementation>()
174173
where TEventTypeImplementation : TEventType { }
175174
}
175+
public class TopicPerEventTopologyOptions : NServiceBus.Transport.AzureServiceBus.TopologyOptions
176+
{
177+
public TopicPerEventTopologyOptions() { }
178+
public bool ThrowIfUnmappedEventTypes { get; set; }
179+
}
176180
[System.Text.Json.Serialization.JsonDerivedType(typeof(NServiceBus.Transport.AzureServiceBus.MigrationTopologyOptions), "migration-topology-options")]
177-
[System.Text.Json.Serialization.JsonDerivedType(typeof(NServiceBus.Transport.AzureServiceBus.TopologyOptions), "topology-options")]
181+
[System.Text.Json.Serialization.JsonDerivedType(typeof(NServiceBus.Transport.AzureServiceBus.TopicPerEventTopologyOptions), "topology-options")]
178182
public class TopologyOptions
179183
{
180184
public TopologyOptions() { }
@@ -186,7 +190,6 @@ namespace NServiceBus.Transport.AzureServiceBus
186190
[NServiceBus.Transport.AzureServiceBus.AzureServiceBusTopics]
187191
[System.Text.Json.Serialization.JsonConverter(typeof(NServiceBus.Transport.AzureServiceBus.SubscribedEventToTopicsMapConverter))]
188192
public System.Collections.Generic.Dictionary<string, System.Collections.Generic.HashSet<string>> SubscribedEventToTopicsMap { get; init; }
189-
public bool ThrowIfUnmappedEventTypes { get; set; }
190193
}
191194
public sealed class TopologyOptionsDisableValidationValidator : Microsoft.Extensions.Options.IValidateOptions<NServiceBus.Transport.AzureServiceBus.TopologyOptions>
192195
{
@@ -207,6 +210,7 @@ namespace NServiceBus.Transport.AzureServiceBus
207210
public System.Text.Json.Serialization.Metadata.JsonTypeInfo<System.Collections.Generic.HashSet<string>> HashSetString { get; }
208211
public System.Text.Json.Serialization.Metadata.JsonTypeInfo<NServiceBus.Transport.AzureServiceBus.MigrationTopologyOptions> MigrationTopologyOptions { get; }
209212
public System.Text.Json.Serialization.Metadata.JsonTypeInfo<string> String { get; }
213+
public System.Text.Json.Serialization.Metadata.JsonTypeInfo<NServiceBus.Transport.AzureServiceBus.TopicPerEventTopologyOptions> TopicPerEventTopologyOptions { get; }
210214
public System.Text.Json.Serialization.Metadata.JsonTypeInfo<NServiceBus.Transport.AzureServiceBus.TopologyOptions> TopologyOptions { get; }
211215
public static NServiceBus.Transport.AzureServiceBus.TopologyOptionsSerializationContext Default { get; }
212216
public override System.Text.Json.Serialization.Metadata.JsonTypeInfo? GetTypeInfo(System.Type type) { }

src/Tests/EventRouting/TopicPerEventSubscriptionManagerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TopicPerEventSubscriptionManagerTests
1313
[Test]
1414
public async Task Should_create_topology_for_mapped_events()
1515
{
16-
var topologyOptions = new TopologyOptions
16+
var topologyOptions = new TopicPerEventTopologyOptions
1717
{
1818
SubscribedEventToTopicsMap =
1919
{
@@ -42,7 +42,7 @@ public async Task Should_create_topology_for_mapped_events()
4242
[Test]
4343
public async Task Should_create_topology_for_unmapped_events()
4444
{
45-
var topologyOptions = new TopologyOptions
45+
var topologyOptions = new TopicPerEventTopologyOptions
4646
{
4747
QueueNameToSubscriptionNameMap = { { "SubscribingQueue", "MySubscriptionName" } },
4848
};

src/Tests/EventRouting/TopicPerEventTopologyTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class TopicPerEventTopologyTests
1010
[Test]
1111
public void PublishDestination_Should_return_mapped_topic_when_event_is_mapped()
1212
{
13-
var topologyOptions = new TopologyOptions
13+
var topologyOptions = new TopicPerEventTopologyOptions
1414
{
1515
PublishedEventToTopicsMap =
1616
{
@@ -28,7 +28,7 @@ public void PublishDestination_Should_return_mapped_topic_when_event_is_mapped()
2828
[Test]
2929
public void PublishDestination_Should_default_topic_to_event_name()
3030
{
31-
var topologyOptions = new TopologyOptions();
31+
var topologyOptions = new TopicPerEventTopologyOptions();
3232

3333
var topology = TopicTopology.FromOptions(topologyOptions);
3434

@@ -40,7 +40,7 @@ public void PublishDestination_Should_default_topic_to_event_name()
4040
[Test]
4141
public void Should_self_validate()
4242
{
43-
var topologyOptions = new TopologyOptions
43+
var topologyOptions = new TopicPerEventTopologyOptions
4444
{
4545
PublishedEventToTopicsMap = { { typeof(MyEvent).FullName, new string('c', 261) } },
4646
SubscribedEventToTopicsMap = { { typeof(MyEvent).FullName, [new string('d', 261), new string('e', 261)] } },
@@ -59,7 +59,7 @@ public void Should_self_validate()
5959
[Test]
6060
public void Should_allow_disabling_validation()
6161
{
62-
var topologyOptions = new TopologyOptions
62+
var topologyOptions = new TopicPerEventTopologyOptions
6363
{
6464
PublishedEventToTopicsMap = { { typeof(MyEvent).FullName, new string('c', 261) } }
6565
};
@@ -73,10 +73,12 @@ public void Should_allow_disabling_validation()
7373
[Test]
7474
public void ThrowIfUnmappedEventTypes_Should_throw_when_type_unmapped()
7575
{
76-
var topologyOptions = new TopologyOptions();
76+
var topologyOptions = new TopicPerEventTopologyOptions
77+
{
78+
ThrowIfUnmappedEventTypes = true
79+
};
7780

7881
var topology = TopicTopology.FromOptions(topologyOptions);
79-
topology.ThrowIfUnmappedEventTypes = true;
8082

8183
Assert.Throws<System.Exception>(() => topology.GetPublishDestination(typeof(MyEvent)));
8284
}

src/Transport/EventRouting/TopicPerEventTopology.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NServiceBus.Transport.AzureServiceBus;
77
/// </summary>
88
public sealed class TopicPerEventTopology : TopicTopology
99
{
10-
internal TopicPerEventTopology(TopologyOptions options) : base(options, new TopologyOptionsValidator())
10+
internal TopicPerEventTopology(TopicPerEventTopologyOptions options) : base(options, new TopologyOptionsValidator())
1111
{
1212
}
1313

@@ -105,14 +105,14 @@ public void OverrideSubscriptionNameFor(string queueName, string subscriptionNam
105105
/// <inheritdoc />
106106
protected override string GetPublishDestinationCore(string eventTypeFullName)
107107
{
108-
if (!Options.PublishedEventToTopicsMap.TryGetValue(eventTypeFullName, out string? topic) && Options.ThrowIfUnmappedEventTypes)
108+
if (!Options.PublishedEventToTopicsMap.TryGetValue(eventTypeFullName, out string? topic) && (Options is TopicPerEventTopologyOptions tpeOptions) && tpeOptions.ThrowIfUnmappedEventTypes)
109109
{
110-
throw new Exception($"Unmapped event type '{eventTypeFullName}'. All events must be mapped in `PublishedEventToTopicsMap` when `ThrowIfUnmappedEventTypes` is set");
110+
throw new Exception($"Unmapped event type '{eventTypeFullName}'. All events must be mapped in `{nameof(TopologyOptions.PublishedEventToTopicsMap)}` when `{nameof(TopicPerEventTopologyOptions.ThrowIfUnmappedEventTypes)}` is set");
111111
}
112112
return topic ?? eventTypeFullName;
113113
}
114114

115115
internal override SubscriptionManager CreateSubscriptionManager(
116116
SubscriptionManagerCreationOptions creationOptions) =>
117-
new TopicPerEventTopologySubscriptionManager(creationOptions, Options);
117+
new TopicPerEventTopologySubscriptionManager(creationOptions, (TopicPerEventTopologyOptions)Options);
118118
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace NServiceBus.Transport.AzureServiceBus;
2+
3+
/// <summary>
4+
/// Serializable object that defines the topic-per-event topology
5+
/// </summary>
6+
public class TopicPerEventTopologyOptions : TopologyOptions
7+
{
8+
/// <summary>
9+
/// Determines if an exception should be thrown when attempting to publish an event not mapped in PublishedEventToTopicsMap
10+
/// </summary>
11+
public bool ThrowIfUnmappedEventTypes { get; set; } = false;
12+
}

src/Transport/EventRouting/TopicPerEventTopologySubscriptionManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace NServiceBus.Transport.AzureServiceBus;
1313

1414
sealed class TopicPerEventTopologySubscriptionManager : SubscriptionManager
1515
{
16-
readonly TopologyOptions topologyOptions;
16+
readonly TopicPerEventTopologyOptions topologyOptions;
1717
readonly string subscriptionName;
1818

1919
public TopicPerEventTopologySubscriptionManager(SubscriptionManagerCreationOptions creationOptions,
20-
TopologyOptions topologyOptions) : base(creationOptions)
20+
TopicPerEventTopologyOptions topologyOptions) : base(creationOptions)
2121
{
2222
this.topologyOptions = topologyOptions;
2323
subscriptionName = topologyOptions.QueueNameToSubscriptionNameMap.GetValueOrDefault(CreationOptions.SubscribingQueueName, CreationOptions.SubscribingQueueName);

src/Transport/EventRouting/TopicTopology.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace NServiceBus
33
using System;
44
using System.ComponentModel.DataAnnotations;
55
using Microsoft.Extensions.Options;
6+
using NServiceBus.Logging;
67
using Particular.Obsoletes;
78
using Transport.AzureServiceBus;
89

@@ -28,33 +29,34 @@ protected TopicTopology(TopologyOptions options, IValidateOptions<TopologyOption
2829

2930
internal TopologyOptions Options { get; }
3031

31-
/// <summary>
32-
/// Determines if an exception should be thrown when attempting to publish an event not mapped in PublishedEventToTopicsMap
33-
/// </summary>
34-
public bool ThrowIfUnmappedEventTypes
35-
{
36-
get => Options.ThrowIfUnmappedEventTypes;
37-
set => Options.ThrowIfUnmappedEventTypes = value;
38-
}
39-
40-
4132
/// <summary>
4233
/// Creates an instance of the topology object based on serializable state.
4334
/// </summary>
4435
/// <param name="options">Serializable topology configuration.</param>
45-
public static TopicTopology FromOptions(TopologyOptions options) =>
46-
options switch
36+
public static TopicTopology FromOptions(TopologyOptions options)
37+
{
38+
static TopicPerEventTopology Default()
39+
{
40+
#pragma warning disable CS0618 // Type or member is obsolete
41+
Log.Warn($"Unknown options type, expected '{nameof(TopicPerEventTopologyOptions)}' or '{nameof(MigrationTopologyOptions)}'");
42+
#pragma warning restore CS0618 // Type or member is obsolete
43+
return TopicTopology.Default;
44+
}
45+
46+
return options switch
4747
{
4848
#pragma warning disable CS0618 // Type or member is obsolete
4949
MigrationTopologyOptions migrationOptions => new MigrationTopology(migrationOptions),
5050
#pragma warning restore CS0618 // Type or member is obsolete
51-
_ => new TopicPerEventTopology(options)
51+
TopicPerEventTopologyOptions tpeOptions => new TopicPerEventTopology(tpeOptions),
52+
_ => Default()
5253
};
54+
}
5355

5456
/// <summary>
5557
/// Returns the default topology that uses topic per event type.
5658
/// </summary>
57-
public static TopicPerEventTopology Default => new(new TopologyOptions());
59+
public static TopicPerEventTopology Default => new(new());
5860

5961
/// <summary>
6062
/// Returns a migration topology using a single topic named <c>bundle-1</c> for <see cref="MigrationTopology.TopicToPublishTo"/> and <see cref="MigrationTopology.TopicToSubscribeOn"/>
@@ -130,5 +132,7 @@ internal string GetPublishDestination(Type eventType)
130132
/// </summary>
131133
/// <param name="eventTypeFullName">Full type name of the event.</param>
132134
protected abstract string GetPublishDestinationCore(string eventTypeFullName);
135+
136+
static ILog Log = LogManager.GetLogger<TopicTopology>();
133137
}
134138
}

src/Transport/EventRouting/TopologyOptions.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace NServiceBus.Transport.AzureServiceBus;
44
using System.Text.Json.Serialization;
55

66
/// <summary>
7-
/// Serializable object that defines the topic-per-event topology
7+
/// Serializable object that defines the topology
88
/// </summary>
9-
[JsonDerivedType(typeof(TopologyOptions), typeDiscriminator: "topology-options")]
9+
[JsonDerivedType(typeof(TopicPerEventTopologyOptions), typeDiscriminator: "topology-options")]
1010
#pragma warning disable CS0618 // Type or member is obsolete
1111
[JsonDerivedType(typeof(MigrationTopologyOptions), typeDiscriminator: "migration-topology-options")]
1212
#pragma warning restore CS0618 // Type or member is obsolete
@@ -43,9 +43,4 @@ public Dictionary<string, string> QueueNameToSubscriptionNameMap
4343
get;
4444
init => field = value ?? [];
4545
} = [];
46-
47-
/// <summary>
48-
/// Determines if an exception should be thrown when attempting to publish an event not mapped in PublishedEventToTopicsMap
49-
/// </summary>
50-
public bool ThrowIfUnmappedEventTypes { get; set; } = false;
5146
}

0 commit comments

Comments
 (0)