Skip to content

Commit

Permalink
fix IReqBinder空绑定问题
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed May 22, 2024
1 parent dd0efda commit 7fe7e48
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Biwen.QuickApi.DemoWeb/Apis/HelloApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public FromBodyRequest()
/// </summary>
public class CustomApiRequestBinder : IReqBinder<HelloApiRequest>
{
public static ValueTask<HelloApiRequest?> BindAsync(HttpContext context, ParameterInfo parameter = null!)
public static ValueTask<HelloApiRequest> BindAsync(HttpContext context, ParameterInfo parameter = null!)
{
var request = new HelloApiRequest();

Expand Down
6 changes: 3 additions & 3 deletions Biwen.QuickApi.DemoWeb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
//app.MapGenQuickApis(app.Services);

//测试其他地方调用QuickApi
app.MapGet("/fromapi", async Task<Results<Ok<string>, BadRequest<IDictionary<string, string[]>>>>
app.MapGet("/fromapi", async Task<Results<Ok<string>, ValidationProblem>>
(JustAsService api) =>
{

Expand All @@ -236,7 +236,7 @@
var result = req.Validate();
if (!result.IsValid)
{
return TypedResults.BadRequest(result.ToDictionary());
return TypedResults.ValidationProblem(result.ToDictionary());
}

//执行请求
Expand Down Expand Up @@ -296,7 +296,7 @@ public partial class BindRequest : BaseRequest<BindRequest>, IReqBinder<BindRequ
{
public string? Hello { get; set; }

public static async ValueTask<BindRequest?> BindAsync(HttpContext context, ParameterInfo parameter = null!)
public static async ValueTask<BindRequest> BindAsync(HttpContext context, ParameterInfo parameter = null!)
{
//返回默认绑定
var req = await DefaultReqBinder<BindRequest>.BindAsync(context, parameter);
Expand Down
2 changes: 1 addition & 1 deletion Biwen.QuickApi/Abstractions/IReqBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace Biwen.QuickApi.Abstractions
/// <typeparam name="T"></typeparam>
public interface IReqBinder<T> where T : class, new()
{
static abstract ValueTask<T?> BindAsync(HttpContext context, ParameterInfo parameter = null!);
static abstract ValueTask<T> BindAsync(HttpContext context, ParameterInfo parameter = null!);
}
}
2 changes: 1 addition & 1 deletion Biwen.QuickApi/Biwen.QuickApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
<PackageVersion>1.6.0</PackageVersion>
<PackageVersion>1.6.1</PackageVersion>
<TargetFrameworks>net8.0</TargetFrameworks>
<PackageProjectUrl>https://github.com/vipwan/Biwen.QuickApi</PackageProjectUrl>
<Authors>万雅虎</Authors>
Expand Down
6 changes: 3 additions & 3 deletions Biwen.QuickApi/DefaultReqBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Biwen.QuickApi
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static async ValueTask<T?> BindAsync(HttpContext context, ParameterInfo parameter = null!)
public static async ValueTask<T> BindAsync(HttpContext context, ParameterInfo parameter = null!)
{

IDictionary<string, object>? ReadFromJsonDic = null;
Expand Down Expand Up @@ -278,9 +278,9 @@ namespace Biwen.QuickApi
/// <typeparam name="T"></typeparam>
internal sealed class EmptyReqBinder<T> : IReqBinder<T> where T : class, new()
{
public static ValueTask<T?> BindAsync(HttpContext context, ParameterInfo parameter = null!)
public static ValueTask<T> BindAsync(HttpContext context, ParameterInfo parameter = null!)
{
return ValueTask.FromResult(default(T));
return ValueTask.FromResult(new T());
}
}

Expand Down
10 changes: 6 additions & 4 deletions Biwen.QuickApi/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,13 @@ async static Task<IResult> RequestHandler(IHttpContextAccessor ctx, Type apiType
//使用接口静态成员重写代码:
MethodInfo methodInfo = (((dynamic)api).ReqBinder).GetMethod("BindAsync", BindingFlags.Static | BindingFlags.Public);
var req = await (dynamic)methodInfo.Invoke(null, [ctx.HttpContext, null])!;

//验证DTO
if (req.Validate() is ValidationResult { IsValid: false } vresult)
if (req is not null)
{
return TypedResults.ValidationProblem(vresult.ToDictionary());
//验证DTO
if (req.Validate() is ValidationResult { IsValid: false } vresult)
{
return TypedResults.ValidationProblem(vresult.ToDictionary());
}
}
var result = await ((dynamic)api)!.ExecuteAsync(req!);
var rawResult = InnerResult(result);
Expand Down

0 comments on commit 7fe7e48

Please sign in to comment.