Skip to content

Commit cce9b77

Browse files
committed
增加接口返回值验证功能
1 parent e819854 commit cce9b77

File tree

5 files changed

+78
-38
lines changed

5 files changed

+78
-38
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using WebApiClient.Contexts;
3+
using Xunit;
4+
5+
namespace WebApiClient.Test.Internal
6+
{
7+
public class ParameterValidatorTest
8+
{
9+
class User
10+
{
11+
[Required]
12+
[StringLength(4)]
13+
public string Account { get; set; }
14+
}
15+
16+
[Fact]
17+
public void ValidateParameterTest()
18+
{
19+
var parameter = TestParameter.Create(null);
20+
Assert.Throws<ValidationException>(() => ApiValidator.ValidateParameter(parameter, true));
21+
22+
parameter = TestParameter.Create(new User { });
23+
Assert.Throws<ValidationException>(() => ApiValidator.ValidateParameter(parameter, true));
24+
25+
parameter = TestParameter.Create(new User { Account = "123" });
26+
ApiValidator.ValidateParameter(parameter, true);
27+
28+
parameter = TestParameter.Create(new User { Account = "123456" });
29+
Assert.Throws<ValidationException>(() => ApiValidator.ValidateParameter(parameter, true));
30+
}
31+
32+
[Fact]
33+
public void ValidateReturnValueTest()
34+
{
35+
var value = default(User);
36+
ApiValidator.ValidateReturnValue(value, true);
37+
38+
value = new User();
39+
Assert.Throws<ValidationException>(() => ApiValidator.ValidateReturnValue(value, true));
40+
41+
value = new User { Account = "123" };
42+
ApiValidator.ValidateReturnValue(value, true);
43+
44+
value = new User { Account = "123456" };
45+
Assert.Throws<ValidationException>(() => ApiValidator.ValidateReturnValue(value, true));
46+
}
47+
}
48+
}

WebApiClient.Test/Internal/ParameterValidatorTest.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

WebApiClient/Contexts/ApiActionContext.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private async Task PrepareRequestAsync()
135135

136136
foreach (var parameter in apiAction.Parameters)
137137
{
138-
ParameterValidator.Validate(parameter, validateProperty);
138+
ApiValidator.ValidateParameter(parameter, validateProperty);
139139
}
140140

141141
foreach (var actionAttribute in apiAction.Attributes)
@@ -172,9 +172,12 @@ private async Task ExecRequestAsync()
172172
.SendAsync(this.RequestMessage, completionOption, cancellation.Token)
173173
.ConfigureAwait(false);
174174

175-
this.Result = await this.ApiActionDescriptor.Return.Attribute
175+
var result = await this.ApiActionDescriptor.Return.Attribute
176176
.GetTaskResult(this)
177177
.ConfigureAwait(false);
178+
179+
ApiValidator.ValidateReturnValue(result, this.HttpApiConfig.UseReturnValuePropertyValidate);
180+
this.Result = result;
178181
}
179182
catch (Exception ex)
180183
{

WebApiClient/HttpApiConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ public Uri HttpHost
9494
/// </summary>
9595
public bool UseParameterPropertyValidate { get; set; } = true;
9696

97+
/// <summary>
98+
/// 获取或设置是否对返回值的属性值进行输入有效性验证
99+
/// 默认为true
100+
/// </summary>
101+
public bool UseReturnValuePropertyValidate { get; set; } = true;
102+
97103
/// <summary>
98104
/// 获取或设置请求时序列化使用的默认格式
99105
/// 影响JsonFormatter或KeyValueFormatter的序列化

WebApiClient/Internal/ParameterValidator.cs renamed to WebApiClient/Internal/ApiValidator.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
namespace WebApiClient
88
{
99
/// <summary>
10-
/// 提供参数值和参数的属性值输入合法性验证
10+
/// Api验证器,提供返回值的属性验证、参数值和参数的属性值验证
1111
/// </summary>
12-
static class ParameterValidator
12+
static class ApiValidator
1313
{
1414
/// <summary>
1515
/// 类型的属性否需要验证缓存
@@ -44,7 +44,7 @@ private static bool IsNeedValidateProperty(object instance)
4444
/// <param name="parameter">参数描述</param>
4545
/// <param name="validateProperty">是否验证属性值</param>
4646
/// <exception cref="ValidationException"></exception>
47-
public static void Validate(ApiParameterDescriptor parameter, bool validateProperty)
47+
public static void ValidateParameter(ApiParameterDescriptor parameter, bool validateProperty)
4848
{
4949
var name = parameter.Name;
5050
var instance = parameter.Value;
@@ -57,7 +57,22 @@ public static void Validate(ApiParameterDescriptor parameter, bool validatePrope
5757
if (validateProperty == true && IsNeedValidateProperty(instance) == true)
5858
{
5959
var ctx = new ValidationContext(instance) { MemberName = name };
60-
Validator.ValidateObject(instance, ctx, true);
60+
Validator.ValidateObject(instance, ctx, false);
61+
}
62+
}
63+
64+
/// <summary>
65+
/// 验证参返回的结果
66+
/// </summary>
67+
/// <param name="value">结果值</param>
68+
/// <param name="validateProperty">是否验证属性值</param>
69+
/// <exception cref="ValidationException"></exception>
70+
public static void ValidateReturnValue(object value, bool validateProperty)
71+
{
72+
if (validateProperty == true && IsNeedValidateProperty(value) == true)
73+
{
74+
var ctx = new ValidationContext(value);
75+
Validator.ValidateObject(value, ctx, false);
6176
}
6277
}
6378
}

0 commit comments

Comments
 (0)