Skip to content

Commit

Permalink
[Host.AsyncApi] Exclude Memory named bus by default and ability to pa…
Browse files Browse the repository at this point in the history
…ss filter method

Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
  • Loading branch information
zarusz committed Apr 25, 2023
1 parent eee201c commit 9fe05cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace SlimMessageBus.Host.AsyncApi;

using Microsoft.Extensions.DependencyInjection.Extensions;

using Saunter.Generation;
Expand All @@ -10,14 +11,20 @@ public static class MessageBusBuilderExtensions
/// </summary>
/// <remarks>This needs to be run before the .AddAsyncApiSchemaGeneration() from Saunter</remarks>
/// <param name="mbb"></param>
/// <param name="busFilter">Provides a filter method to exclude or include a (child) bus from AsyncAPI document generation. When not provided will filter out bus named Memory</param>
/// <returns></returns>
public static MessageBusBuilder AddAsyncApiDocumentGenerator(this MessageBusBuilder mbb)
public static MessageBusBuilder AddAsyncApiDocumentGenerator(this MessageBusBuilder mbb, Func<MessageBusSettings, bool>? busFilter = null)
{
mbb.Settings.Properties[BusFilter] = busFilter
?? (x => x.Name != "Memory");

mbb.PostConfigurationActions.Add(services =>
{
services.TryAddTransient<IDocumentGenerator, MessageBusDocumentGenerator>();
});

return mbb;
}

internal const string BusFilter = "AsyncAPI_BusFilter";
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ public AsyncApiDocument GenerateDocument(TypeInfo[] asyncApiTypes, AsyncApiOptio

var generator = new JsonSchemaGenerator(options.SchemaOptions);

var busFilter = _busSettings.GetOrDefault<Func<MessageBusSettings, bool>?>(MessageBusBuilderExtensions.BusFilter, null);

// ToDo: implement
var serverByBusSettings = new Dictionary<MessageBusSettings, NamedServer>
{
{ _busSettings, new("root", new Server("kafka://kafka.cluster", "kafka")) }
};
foreach (var childMessageBusSettings in _busSettings.Children)
{
serverByBusSettings.Add(childMessageBusSettings, new(childMessageBusSettings.Name, new Server("kafka://kafka.cluster", "kafka")));
if (busFilter == null || busFilter(childMessageBusSettings))
{
serverByBusSettings.Add(childMessageBusSettings, new(childMessageBusSettings.Name, new Server("kafka://kafka.cluster", "kafka")));
}
}

foreach (var (messageBusSettings, namedServer) in serverByBusSettings)
Expand Down

0 comments on commit 9fe05cb

Please sign in to comment.