From 9fe05cbfccf22f85cb66da8ba04f481cc0adbb52 Mon Sep 17 00:00:00 2001 From: Tomasz Maruszak Date: Tue, 25 Apr 2023 23:57:10 +0200 Subject: [PATCH] [Host.AsyncApi] Exclude Memory named bus by default and ability to pass filter method Signed-off-by: Tomasz Maruszak --- .../MessageBusBuilderExtensions.cs | 9 ++++++++- .../MessageBusDocumentGenerator.cs | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/SlimMessageBus.Host.AsyncApi/MessageBusBuilderExtensions.cs b/src/SlimMessageBus.Host.AsyncApi/MessageBusBuilderExtensions.cs index a268dae7..3ed320f4 100644 --- a/src/SlimMessageBus.Host.AsyncApi/MessageBusBuilderExtensions.cs +++ b/src/SlimMessageBus.Host.AsyncApi/MessageBusBuilderExtensions.cs @@ -1,4 +1,5 @@ namespace SlimMessageBus.Host.AsyncApi; + using Microsoft.Extensions.DependencyInjection.Extensions; using Saunter.Generation; @@ -10,9 +11,13 @@ public static class MessageBusBuilderExtensions /// /// This needs to be run before the .AddAsyncApiSchemaGeneration() from Saunter /// + /// Provides a filter method to exclude or include a (child) bus from AsyncAPI document generation. When not provided will filter out bus named Memory /// - public static MessageBusBuilder AddAsyncApiDocumentGenerator(this MessageBusBuilder mbb) + public static MessageBusBuilder AddAsyncApiDocumentGenerator(this MessageBusBuilder mbb, Func? busFilter = null) { + mbb.Settings.Properties[BusFilter] = busFilter + ?? (x => x.Name != "Memory"); + mbb.PostConfigurationActions.Add(services => { services.TryAddTransient(); @@ -20,4 +25,6 @@ public static MessageBusBuilder AddAsyncApiDocumentGenerator(this MessageBusBuil return mbb; } + + internal const string BusFilter = "AsyncAPI_BusFilter"; } diff --git a/src/SlimMessageBus.Host.AsyncApi/MessageBusDocumentGenerator.cs b/src/SlimMessageBus.Host.AsyncApi/MessageBusDocumentGenerator.cs index 1f9ecb17..5b10cc79 100644 --- a/src/SlimMessageBus.Host.AsyncApi/MessageBusDocumentGenerator.cs +++ b/src/SlimMessageBus.Host.AsyncApi/MessageBusDocumentGenerator.cs @@ -39,6 +39,8 @@ public AsyncApiDocument GenerateDocument(TypeInfo[] asyncApiTypes, AsyncApiOptio var generator = new JsonSchemaGenerator(options.SchemaOptions); + var busFilter = _busSettings.GetOrDefault?>(MessageBusBuilderExtensions.BusFilter, null); + // ToDo: implement var serverByBusSettings = new Dictionary { @@ -46,7 +48,10 @@ public AsyncApiDocument GenerateDocument(TypeInfo[] asyncApiTypes, AsyncApiOptio }; 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)