Skip to content

Commit

Permalink
TrackingHelper优化、swagger分组
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldairarrow committed Aug 8, 2023
1 parent 5c2e36c commit 6917fbb
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 46 deletions.
1 change: 1 addition & 0 deletions demos/Demo.AspnetCore/Controllers/HomeController.cs
Expand Up @@ -6,6 +6,7 @@ namespace Demo.AspnetCore.Controllers
{
[ApiController]
[Route("[controller]")]
[ApiExplorerSettings(GroupName = "default")]
public class HomeController : ControllerBase
{
private readonly IDistributedId _distributedId;
Expand Down
3 changes: 3 additions & 0 deletions demos/Demo.AspnetCore/Controllers/UserController.cs
Expand Up @@ -2,11 +2,14 @@
using Demo.AspnetCore.Dtos;
using Demo.AspnetCore.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using System;

namespace Demo.AspnetCore.Controllers;

[AllowAnonymous]
[ApiExplorerSettings(GroupName = "group1")]
public class UserController : CRUDControllerBase<long, User, UserDto, UserExtendDto, UserSearchDto, DemoDbContext>
{
public UserController(IServiceProvider serviceProvider) : base(serviceProvider)
Expand Down
27 changes: 0 additions & 27 deletions demos/Demo.AspnetCore/MailService.cs

This file was deleted.

3 changes: 0 additions & 3 deletions demos/Demo.AspnetCore/Program.cs
@@ -1,5 +1,4 @@
using Colder.Api.Abstractions;
using Demo.AspnetCore;
using Demo.AspnetCore.Entities;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
Expand All @@ -10,6 +9,4 @@
{
services.AddDbContext<DemoDbContext>(x => x.UseSqlite("Data Source=db.db"));
services.AddAutoMapper(Assembly.GetExecutingAssembly());
services.AddHostedService<MailService>();
});
18 changes: 15 additions & 3 deletions demos/Demo.AspnetCore/appsettings.json
Expand Up @@ -32,8 +32,20 @@
]
},
"api": {
"EnableSwagger": false,
"EnableJwt": true,
"JwtSecret": null
"EnableSwagger": true,
"EnableJwt": false,
"JwtSecret": null,
"Documents": [
{
"Title": "default",
"DocumentName": "default",
"ApiGroupNames": [ "default" ]
},
{
"Title": "group1",
"DocumentName": "group1",
"ApiGroupNames": [ "group1" ]
}
]
}
}
50 changes: 40 additions & 10 deletions src/Colder.Api.Abstractions/ApiExtentions.cs
@@ -1,4 +1,5 @@
using Colder.DistributedId;
using Colder.Api.Abstractions.Options;
using Colder.DistributedId;
using Colder.Json;
using Colder.Logging.Serilog;
using Logistics.Api;
Expand All @@ -9,6 +10,7 @@
using Microsoft.Extensions.Options;
using NSwag;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Colder.Api.Abstractions;
Expand Down Expand Up @@ -51,6 +53,7 @@ public static IHostBuilder ConfigureWebApiDefaults(this IHostBuilder hostBuilder
x.EnableSwagger = apiOption.EnableSwagger;
x.EnableJwt = apiOption.EnableJwt;
x.JwtSecret = apiOption.JwtSecret;
x.Documents = apiOption.Documents;
});
}
else
Expand Down Expand Up @@ -90,18 +93,45 @@ public static IHostBuilder ConfigureWebApiDefaults(this IHostBuilder hostBuilder
});
//swagger
services.AddOpenApiDocument(settings =>
List<SwaggerDocumentOptions> documents = apiOption.Documents?.ToList() ?? new List<SwaggerDocumentOptions>();
if (documents.Count == 0)
{
settings.AllowReferencesWithProperties = true;
settings.AddSecurity("身份认证Token", Enumerable.Empty<string>(), new OpenApiSecurityScheme()
documents.Add(new SwaggerDocumentOptions());
}
apiOption.Documents = documents.ToArray();
foreach (var config in apiOption.Documents)
{
services.AddOpenApiDocument(options =>
{
Scheme = "bearer",
Description = "Authorization:Bearer {your JWT token}",
Name = "Authorization",
In = OpenApiSecurityApiKeyLocation.Header,
Type = OpenApiSecuritySchemeType.Http
if (!string.IsNullOrEmpty(config.Title))
{
options.Title = config.Title;
}
if (!string.IsNullOrEmpty(config.DocumentName))
{
options.DocumentName = config.DocumentName;
}
if (config.ApiGroupNames.Length > 0)
{
options.ApiGroupNames = config.ApiGroupNames;
}
options.SchemaProcessors.Add(new EnumSchemaProcessor());
//解决枚举无法展示问题
options.AllowReferencesWithProperties = true;
options.AddSecurity("身份认证Token", Enumerable.Empty<string>(), new OpenApiSecurityScheme()
{
Scheme = "bearer",
Description = "Authorization:Bearer {your JWT token}",
Name = "Authorization",
In = OpenApiSecurityApiKeyLocation.Header,
Type = OpenApiSecuritySchemeType.Http
});
});
});
}
if (apiOption.EnableJwt)
{
Expand Down
44 changes: 44 additions & 0 deletions src/Colder.Api.Abstractions/EnumSchemaProcessor.cs
@@ -0,0 +1,44 @@
using Namotion.Reflection;
using NJsonSchema.Generation;
using System;
using System.Linq;
using System.Text;

namespace Colder.Api.Abstractions;

/// <summary>
///
/// </summary>
public class EnumSchemaProcessor : ISchemaProcessor
{
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public void Process(SchemaProcessorContext context)
{
var type = context.ContextualType.Type;
if (!type.IsEnum)
{
return;
}

var sb = new StringBuilder();
sb.AppendLine(type.GetXmlDocsSummary());

var members = type.GetMembers();
var schema = context.Schema;
for (var i = 0; i < schema.Enumeration.Count; i++)
{
var item = schema.Enumeration.ElementAt(i);
var enumName = Enum.GetName(type, item);

var summary = members.FirstOrDefault(a => a.Name == enumName)
.GetXmlDocsSummary();

sb.AppendLine($"{item} = {summary}");
}

schema.Description = sb.ToString();
}
}
3 changes: 2 additions & 1 deletion src/Colder.Api.Abstractions/JwtExtentions.cs
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Colder.Api.Abstractions.Options;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using System;
Expand Down
@@ -1,4 +1,6 @@
namespace Colder.Api.Abstractions;
using System;

namespace Colder.Api.Abstractions.Options;

/// <summary>
///
Expand All @@ -19,4 +21,9 @@ public class ApiOptions
/// 启用Swagger
/// </summary>
public bool EnableSwagger { get; set; } = true;

/// <summary>
/// 文档分组
/// </summary>
public SwaggerDocumentOptions[] Documents { get; set; } = Array.Empty<SwaggerDocumentOptions>();
}
24 changes: 24 additions & 0 deletions src/Colder.Api.Abstractions/Options/SwaggerDocumentOptions.cs
@@ -0,0 +1,24 @@
using System;

namespace Colder.Api.Abstractions.Options;

/// <summary>
/// Swagger 文档配置。
/// </summary>
public class SwaggerDocumentOptions
{
/// <summary>
/// 获取或设置 Swagger 标题。
/// </summary>
public string Title { get; set; }

/// <summary>
/// 获取或设置文档名称。
/// </summary>
public string DocumentName { get; set; }

/// <summary>
/// 获取或设置 API 分组名称。
/// </summary>
public string[] ApiGroupNames { get; set; } = Array.Empty<string>();
}
2 changes: 1 addition & 1 deletion src/Colder.Common/Helpers/TrackingHelper.cs
Expand Up @@ -245,7 +245,7 @@ private static bool NeedTracking(MemberInfo type)
var needTracking = type.GetCustomAttribute<NotMappedAttribute>() == null
&& type.GetCustomAttribute<ConcurrencyCheckAttribute>() == null;

if (type is PropertyInfo property)
if (type is PropertyInfo property && !IsEntityClass(property.PropertyType) && !IsEntityCollection(property.PropertyType))
{
needTracking = needTracking && property.CanWrite;
}
Expand Down

0 comments on commit 6917fbb

Please sign in to comment.