From 81e68ef5ae0610ce3cb3276c8aa0e056cd6388ae Mon Sep 17 00:00:00 2001 From: Azhe Kun Date: Sat, 24 Feb 2024 10:07:24 +0700 Subject: [PATCH] add SwaggerIgnore --- .../Core/ApiRequestBase.cs | 9 ++++++ .../Group/DeleteWelcomeMessageRequest.cs | 1 + .../Handlers/RestApis/Note/SaveNoteRequest.cs | 1 + .../SwaggerIgnoreAttribute.cs | 6 ++++ backend/ZiziBot.Utils/StringUtil.cs | 12 ++++++++ .../Controllers/WebhookController.cs | 1 + .../Providers/SwaggerIgnoreFilter.cs | 28 +++++++++++++++++++ backend/ZiziBot.WebApi/RestApiExtension.cs | 6 ++++ 8 files changed, 64 insertions(+) create mode 100644 backend/ZiziBot.Attributes/SwaggerIgnoreAttribute.cs create mode 100644 backend/ZiziBot.WebApi/Providers/SwaggerIgnoreFilter.cs diff --git a/backend/ZiziBot.Application/Core/ApiRequestBase.cs b/backend/ZiziBot.Application/Core/ApiRequestBase.cs index 6e370a6c..aaf789ad 100644 --- a/backend/ZiziBot.Application/Core/ApiRequestBase.cs +++ b/backend/ZiziBot.Application/Core/ApiRequestBase.cs @@ -7,29 +7,38 @@ namespace ZiziBot.Application.Core; public class ApiRequestBase : IRequest> { [FromServices] + [SwaggerIgnore] public IHttpContextAccessor? HttpContextAccessor { get; set; } [BindNever] + [SwaggerIgnore] public IHeaderDictionary Headers => HttpContextAccessor?.HttpContext?.Request.Headers!; [BindNever] + [SwaggerIgnore] public string? TransactionId => Headers[HeaderKey.TransactionId]; [BindNever] + [SwaggerIgnore] public string? Authorization => Headers[HeaderKey.Authorization]; [BindNever] + [SwaggerIgnore] public long SessionUserId => Headers.GetUserId(); [BindNever] + [SwaggerIgnore] public ApiRole? SessionUserRole => Headers[HeaderKey.UserRole].ToString().ToEnum(ApiRole.Guest); [BindNever] + [SwaggerIgnore] public List AdminChatId => Headers[HeaderKey.ListChatId].ToString().ToObject>() ?? new List(); [BindNever] + [SwaggerIgnore] public List ListChatId => AdminChatId.Append(SessionUserId).Where(x => x != 0).ToList(); [BindNever] + [SwaggerIgnore] public string? BearerToken => Authorization?.Replace("Bearer ", string.Empty); } \ No newline at end of file diff --git a/backend/ZiziBot.Application/Handlers/RestApis/Group/DeleteWelcomeMessageRequest.cs b/backend/ZiziBot.Application/Handlers/RestApis/Group/DeleteWelcomeMessageRequest.cs index cc36cc0a..755fc369 100644 --- a/backend/ZiziBot.Application/Handlers/RestApis/Group/DeleteWelcomeMessageRequest.cs +++ b/backend/ZiziBot.Application/Handlers/RestApis/Group/DeleteWelcomeMessageRequest.cs @@ -27,6 +27,7 @@ public class DeleteWelcomeMessageRequestModel public long ChatId { get; set; } [BindNever] + [SwaggerIgnore] public ObjectId ObjectId => ObjectId.Parse(Id); } diff --git a/backend/ZiziBot.Application/Handlers/RestApis/Note/SaveNoteRequest.cs b/backend/ZiziBot.Application/Handlers/RestApis/Note/SaveNoteRequest.cs index dd8d67bb..f155664d 100644 --- a/backend/ZiziBot.Application/Handlers/RestApis/Note/SaveNoteRequest.cs +++ b/backend/ZiziBot.Application/Handlers/RestApis/Note/SaveNoteRequest.cs @@ -23,6 +23,7 @@ public class SaveNoteRequestModel public int DataType { get; set; } = -1; [BindNever] + [SwaggerIgnore] public ObjectId ObjectId => Id != null ? new ObjectId(Id) : ObjectId.Empty; } diff --git a/backend/ZiziBot.Attributes/SwaggerIgnoreAttribute.cs b/backend/ZiziBot.Attributes/SwaggerIgnoreAttribute.cs new file mode 100644 index 00000000..beb97561 --- /dev/null +++ b/backend/ZiziBot.Attributes/SwaggerIgnoreAttribute.cs @@ -0,0 +1,6 @@ +namespace ZiziBot.Attributes; + +[AttributeUsage(AttributeTargets.Property)] +public class SwaggerIgnoreAttribute : Attribute +{ +} \ No newline at end of file diff --git a/backend/ZiziBot.Utils/StringUtil.cs b/backend/ZiziBot.Utils/StringUtil.cs index f50081eb..d13c9299 100644 --- a/backend/ZiziBot.Utils/StringUtil.cs +++ b/backend/ZiziBot.Utils/StringUtil.cs @@ -174,4 +174,16 @@ public static string RegexMatchIf(this string input, string pattern, bool condit { return condition ? input.RegexMatch(pattern) : input; } + + public static string ToCamelCase(this string input) + { + if (string.IsNullOrEmpty(input) || char.IsLower(input[0])) + { + return input; + } + var chars = input.ToCharArray(); + chars[0] = char.ToLowerInvariant(input[0]); + + return new(chars); + } } \ No newline at end of file diff --git a/backend/ZiziBot.WebApi/Controllers/WebhookController.cs b/backend/ZiziBot.WebApi/Controllers/WebhookController.cs index cd4a8450..82bd77bb 100644 --- a/backend/ZiziBot.WebApi/Controllers/WebhookController.cs +++ b/backend/ZiziBot.WebApi/Controllers/WebhookController.cs @@ -7,6 +7,7 @@ namespace ZiziBot.WebApi.Controllers; public class WebhookController : ApiControllerBase { [HttpPost("{targetId}")] + [ApiExplorerSettings(IgnoreApi = true)] [AccessFilter(checkHeader: false)] public async Task ProcessingPayload(PostWebhookPayloadRequest request) { diff --git a/backend/ZiziBot.WebApi/Providers/SwaggerIgnoreFilter.cs b/backend/ZiziBot.WebApi/Providers/SwaggerIgnoreFilter.cs new file mode 100644 index 00000000..88f01add --- /dev/null +++ b/backend/ZiziBot.WebApi/Providers/SwaggerIgnoreFilter.cs @@ -0,0 +1,28 @@ +using System.Reflection; +using System.Text.Json.Serialization; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; +using ZiziBot.Attributes; +using ZiziBot.Utils; + +namespace ZiziBot.WebApi.Providers; + +public class SwaggerIgnoreFilter : ISchemaFilter +{ + public void Apply(OpenApiSchema schema, SchemaFilterContext context) + { + if (schema.Properties.Count == 0) + { + return; + } + var properties = context.Type.GetProperties(); + var excludedList = properties + .Where(m => m.GetCustomAttribute() is not null) + .Select(m => m.GetCustomAttribute()?.Name ?? m.Name.ToCamelCase()); + + foreach (var excludedName in excludedList) + { + schema.Properties.Remove(excludedName); + } + } +} \ No newline at end of file diff --git a/backend/ZiziBot.WebApi/RestApiExtension.cs b/backend/ZiziBot.WebApi/RestApiExtension.cs index 6c6a4051..c77129ad 100644 --- a/backend/ZiziBot.WebApi/RestApiExtension.cs +++ b/backend/ZiziBot.WebApi/RestApiExtension.cs @@ -105,6 +105,7 @@ await context.Response.WriteAsJsonAsync( services.AddEndpointsApiExplorer(); services.AddSwaggerGen(options => { // options.DocumentFilter("/api"); + options.SchemaFilter(); } ); @@ -152,6 +153,11 @@ public static WebApplication ConfigureApi(this WebApplication app) app.ConfigureRateLimiter(); + app.UseSwagger(); + app.UseSwaggerUI(options => { + options.DefaultModelsExpandDepth(-1); + }); + return app; } } \ No newline at end of file