Skip to content

Commit

Permalink
add SwaggerIgnore
Browse files Browse the repository at this point in the history
  • Loading branch information
azhe403 committed Feb 24, 2024
1 parent 0deefd4 commit 81e68ef
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions backend/ZiziBot.Application/Core/ApiRequestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,38 @@ namespace ZiziBot.Application.Core;
public class ApiRequestBase<T> : IRequest<ApiResponseBase<T>>
{
[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<long> AdminChatId => Headers[HeaderKey.ListChatId].ToString().ToObject<List<long>>() ?? new List<long>();

[BindNever]
[SwaggerIgnore]
public List<long> ListChatId => AdminChatId.Append(SessionUserId).Where(x => x != 0).ToList();

[BindNever]
[SwaggerIgnore]
public string? BearerToken => Authorization?.Replace("Bearer ", string.Empty);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class DeleteWelcomeMessageRequestModel
public long ChatId { get; set; }

[BindNever]
[SwaggerIgnore]
public ObjectId ObjectId => ObjectId.Parse(Id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions backend/ZiziBot.Attributes/SwaggerIgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ZiziBot.Attributes;

[AttributeUsage(AttributeTargets.Property)]
public class SwaggerIgnoreAttribute : Attribute
{
}
12 changes: 12 additions & 0 deletions backend/ZiziBot.Utils/StringUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 1 addition & 0 deletions backend/ZiziBot.WebApi/Controllers/WebhookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace ZiziBot.WebApi.Controllers;
public class WebhookController : ApiControllerBase
{
[HttpPost("{targetId}")]
[ApiExplorerSettings(IgnoreApi = true)]
[AccessFilter(checkHeader: false)]
public async Task<IActionResult> ProcessingPayload(PostWebhookPayloadRequest request)
{
Expand Down
28 changes: 28 additions & 0 deletions backend/ZiziBot.WebApi/Providers/SwaggerIgnoreFilter.cs
Original file line number Diff line number Diff line change
@@ -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<SwaggerIgnoreAttribute>() is not null)
.Select(m => m.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name ?? m.Name.ToCamelCase());

foreach (var excludedName in excludedList)
{
schema.Properties.Remove(excludedName);
}
}
}
6 changes: 6 additions & 0 deletions backend/ZiziBot.WebApi/RestApiExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ await context.Response.WriteAsJsonAsync(
services.AddEndpointsApiExplorer();
services.AddSwaggerGen(options => {
// options.DocumentFilter<PathPrefixSwaggerDocumentFilter>("/api");
options.SchemaFilter<SwaggerIgnoreFilter>();
}
);

Expand Down Expand Up @@ -152,6 +153,11 @@ public static WebApplication ConfigureApi(this WebApplication app)

app.ConfigureRateLimiter();

app.UseSwagger();
app.UseSwaggerUI(options => {
options.DefaultModelsExpandDepth(-1);
});

return app;
}
}

0 comments on commit 81e68ef

Please sign in to comment.