Skip to content

Commit

Permalink
🐾 break changes and close #I24B8P.
Browse files Browse the repository at this point in the history
  • Loading branch information
MonkSoul committed Nov 7, 2020
1 parent 15ee1f3 commit b200a78
Show file tree
Hide file tree
Showing 21 changed files with 348 additions and 224 deletions.
1 change: 1 addition & 0 deletions framework/Fur.Application/Persons/PersonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public async Task<PersonDto> Find(int id)
/// 查询所有
/// </summary>
/// <returns></returns>
//[NonUnify]
public async Task<List<PersonDto>> GetAll()
{
var persons = _personRepository.AsQueryable(false)
Expand Down
1 change: 1 addition & 0 deletions framework/Fur.Application/RBAC/RBACService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
Expand Down
12 changes: 11 additions & 1 deletion framework/Fur/App/Extensions/AppApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Builder
public static class AppApplicationBuilderExtensions
{
/// <summary>
/// 注入基础中间件
/// 注入基础中间件(带Swagger)
/// </summary>
/// <param name="app"></param>
/// <param name="routePrefix"></param>
Expand All @@ -22,6 +22,16 @@ public static IApplicationBuilder UseInject(this IApplicationBuilder app, string
return app;
}

/// <summary>
/// 注入基础中间件
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static IApplicationBuilder UseInjectBase(this IApplicationBuilder app)
{
return app;
}

/// <summary>
/// 添加应用中间件
/// </summary>
Expand Down
16 changes: 15 additions & 1 deletion framework/Fur/App/Extensions/AppServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class AppServiceCollectionExtensions
private const string MiniProfilerRouteBasePath = "/index-mini-profiler";

/// <summary>
/// Mvc 注入基础配置
/// Mvc 注入基础配置(带Swagger)
/// </summary>
/// <param name="mvcBuilder">Mvc构建起</param>
/// <returns>IMvcBuilder</returns>
Expand All @@ -36,6 +36,20 @@ public static IMvcBuilder AddInject(this IMvcBuilder mvcBuilder)
return mvcBuilder;
}

/// <summary>
/// Mvc 注入基础配置
/// </summary>
/// <param name="mvcBuilder">Mvc构建起</param>
/// <returns>IMvcBuilder</returns>
public static IMvcBuilder AddInjectBase(this IMvcBuilder mvcBuilder)
{
mvcBuilder.AddDynamicApiControllers()
.AddDataValidation()
.AddFriendlyException();

return mvcBuilder;
}

/// <summary>
/// Mvc 注入基础配置和规范化结果
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Fur.DependencyInjection;
using System.ComponentModel.DataAnnotations;
using Fur.DataValidation;
using Fur.DependencyInjection;
using System.Linq;

namespace Fur.DataValidation
namespace System.ComponentModel.DataAnnotations
{
/// <summary>
/// 数据类型验证特性
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Fur.DependencyInjection;
using System;

namespace Fur.DataValidation
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// 跳过验证
Expand Down
2 changes: 1 addition & 1 deletion framework/Fur/DataValidation/Enums/ValidationPattern.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Fur.DependencyInjection;

namespace Fur.DataValidation
namespace System.ComponentModel.DataAnnotations
{
/// <summary>
/// 验证逻辑
Expand Down
16 changes: 6 additions & 10 deletions framework/Fur/DataValidation/Filters/DataValidationFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Net.Mime;
Expand Down Expand Up @@ -91,7 +90,7 @@ public void OnActionExecuting(ActionExecutingContext context)
if (context.Result == null && !modelState.IsValid)
{
// 设置验证失败结果
SetValidateFailedResult(context, modelState);
SetValidateFailedResult(context, modelState, actionDescriptor);
}
}

Expand All @@ -100,7 +99,8 @@ public void OnActionExecuting(ActionExecutingContext context)
/// </summary>
/// <param name="context">动作方法执行上下文</param>
/// <param name="modelState">模型验证状态</param>
private void SetValidateFailedResult(ActionExecutingContext context, ModelStateDictionary modelState)
/// <param name="actionDescriptor"></param>
private void SetValidateFailedResult(ActionExecutingContext context, ModelStateDictionary modelState, ControllerActionDescriptor actionDescriptor)
{
// 将验证错误信息转换成字典并序列化成 Json
var validationResults = modelState.ToDictionary(u => u.Key, u => modelState[u.Key].Errors.Select(c => c.ErrorMessage));
Expand All @@ -110,13 +110,8 @@ private void SetValidateFailedResult(ActionExecutingContext context, ModelStateD
WriteIndented = true
});

// 处理规范化结果
var unifyResult = _serviceProvider.GetService<IUnifyResultProvider>();
if (unifyResult != null)
{
context.Result = unifyResult.OnValidateFailed(context, modelState, validationResults, validateFaildMessage);
}
else
// 判断是否跳过规范化结果
if (UnifyResultContext.IsSkipUnifyHandler(actionDescriptor.MethodInfo, out var unifyResult))
{
// 返回 400 错误
var result = new BadRequestObjectResult(modelState);
Expand All @@ -127,6 +122,7 @@ private void SetValidateFailedResult(ActionExecutingContext context, ModelStateD

context.Result = result;
}
else context.Result = unifyResult.OnValidateFailed(context, modelState, validationResults, validateFaildMessage);

// 打印验证失败信息
App.PrintToMiniProfiler(MiniProfilerCategory, "Failed", $"Validation Failed:\r\n{validateFaildMessage}", true);
Expand Down
1 change: 1 addition & 0 deletions framework/Fur/DataValidation/Validators/DataValidator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Fur.DependencyInjection;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Fur.DependencyInjection;
using Microsoft.AspNetCore.Mvc;
using Fur.DynamicApiController;
using System;

namespace Fur.DynamicApiController
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// 接口描述设置
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Fur.DependencyInjection;
using System;

namespace Fur.DynamicApiController
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// 接口参数位置设置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Routing;
using System;
Expand Down Expand Up @@ -488,9 +486,8 @@ private static bool CheckIsSplitCamelCase(ApiDescriptionSettingsAttribute apiDes
/// <param name="action"></param>
private static void ConfigureActionUnifyResultAttribute(ActionModel action)
{
// 判断是否手动添加了标注
if (action.Attributes.Any(x => typeof(ProducesResponseTypeAttribute).IsAssignableFrom(x.GetType())
|| typeof(IApiResponseMetadataProvider).IsAssignableFrom(x.GetType()))) return;
// 判断是否手动添加了标注或跳过规范化处理
if (UnifyResultContext.IsSkipOnSuccessUnifyHandler(action.ActionMethod, out var _)) return;

// 获取真实类型
var returnType = action.ActionMethod.GetMethodRealReturnType();
Expand Down
2 changes: 1 addition & 1 deletion framework/Fur/DynamicApiController/Enums/ApiSeats.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Fur.DependencyInjection;

namespace Fur.DynamicApiController
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// 接口参数位置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ public async Task OnExceptionAsync(ExceptionContext context)
var validationFlag = "[Validation]";
var errorMessage = exception.Message.StartsWith(validationFlag) ? exception.Message[validationFlag.Length..] : exception.Message;

// 处理规范化结果
var unifyResult = _serviceProvider.GetService<IUnifyResultProvider>();
context.Result = unifyResult == null
? new ContentResult
// 判断是否跳过规范化结果
if (UnifyResultContext.IsSkipUnifyHandler(actionDescriptor.MethodInfo, out var unifyResult))
{
context.Result = new ContentResult
{
Content = errorMessage,
StatusCode = exception.Message.StartsWith(validationFlag) ? StatusCodes.Status400BadRequest : StatusCodes.Status500InternalServerError
}
: unifyResult.OnException(context);
};
}
else context.Result = unifyResult.OnException(context);

// 处理验证异常,打印验证失败信息
if (exception.Message.StartsWith(validationFlag))
Expand Down
2 changes: 1 addition & 1 deletion framework/Fur/Fur.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RepositoryUrl>https://gitee.com/monksoul/Fur</RepositoryUrl>
<RepositoryType>Gitee</RepositoryType>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Version>1.0.0-rc.final.87</Version>
<Version>1.0.0-rc.final.91</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageIcon>icon.png</PackageIcon>
</PropertyGroup>
Expand Down
Loading

0 comments on commit b200a78

Please sign in to comment.