-
Notifications
You must be signed in to change notification settings - Fork 177
/
QueryFieldAttribute.cs
169 lines (150 loc) · 8.24 KB
/
QueryFieldAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Linq;
using System.Threading.Tasks;
using EmbedIO.Utilities;
using Swan;
namespace EmbedIO.WebApi
{
/// <summary>
/// <para>Specifies that a parameter of a controller method will receive the value of a field,
/// obtained by deserializing a request URL query.</para>
/// <para>The parameter carrying this attribute can be either a simple type or a one-dimension array.</para>
/// <para>If multiple values are present for the field, a non-array parameter will receive the last specified value,
/// while an array parameter will receive an array of field values converted to the element type of the
/// parameter.</para>
/// <para>If a single value is present for the field, a non-array parameter will receive the value converted
/// to the type of the parameter, while an array parameter will receive an array of length 1, containing
/// the value converted to the element type of the parameter</para>
/// <para>If no values are present for the field and the <see cref="BadRequestIfMissing"/> property is
/// <see langword="true" />, a <c>400 Bad Request</c> response will be sent to the client, with a message
/// specifying the name of the missing field.</para>
/// <para>If no values are present for the field and the <see cref="BadRequestIfMissing"/> property is
/// <see langword="false" />, a non-array parameter will receive the default value for its type, while
/// an array parameter will receive an array of length 0.</para>
/// <para>This class cannot be inherited.</para>
/// </summary>
/// <seealso cref="Attribute" />
/// <seealso cref="IRequestDataAttribute{TController,TData}" />
/// <seealso cref="IRequestDataAttribute{TController}" />
[AttributeUsage(AttributeTargets.Parameter)]
public sealed class QueryFieldAttribute :
Attribute,
IRequestDataAttribute<WebApiController, string>,
IRequestDataAttribute<WebApiController, string[]>,
IRequestDataAttribute<WebApiController>
{
/// <summary>
/// Initializes a new instance of the <see cref="QueryFieldAttribute"/> class.
/// The name of the query field to extract will be equal to the name of the parameter
/// carrying this attribute.
/// </summary>
public QueryFieldAttribute()
: this(false, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryFieldAttribute"/> class.
/// </summary>
/// <param name="fieldName">The name of the query field to extract.</param>
/// <exception cref="ArgumentNullException"><paramref name="fieldName"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="fieldName"/> is the empty string (<c>""</c>).</exception>
public QueryFieldAttribute(string fieldName)
: this(false, Validate.NotNullOrEmpty(nameof(fieldName), fieldName))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryFieldAttribute" /> class.
/// The name of the query field to extract will be equal to the name of the parameter
/// carrying this attribute.
/// </summary>
/// <param name="badRequestIfMissing">If set to <see langword="true" />, a <c>400 Bad Request</c>
/// response will be sent to the client if no values are found for the field; if set to
/// <see langword="false" />, a default value will be assumed.</param>
public QueryFieldAttribute(bool badRequestIfMissing)
: this(badRequestIfMissing, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryFieldAttribute"/> class.
/// </summary>
/// <param name="fieldName">The name of the query field to extract.</param>
/// <exception cref="ArgumentNullException"><paramref name="fieldName"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="fieldName"/> is the empty string (<c>""</c>).</exception>
/// <param name="badRequestIfMissing">If set to <see langword="true" />, a <c>400 Bad Request</c>
/// response will be sent to the client if no values are found for the field; if set to
/// <see langword="false" />, a default value will be assumed.</param>
public QueryFieldAttribute(string fieldName, bool badRequestIfMissing)
: this(badRequestIfMissing, Validate.NotNullOrEmpty(nameof(fieldName), fieldName))
{
}
private QueryFieldAttribute(bool badRequestIfMissing, string? fieldName)
{
BadRequestIfMissing = badRequestIfMissing;
FieldName = fieldName;
}
/// <summary>
/// Gets the name of the query field that this attribute will extract,
/// or <see langword="null" /> if the name of the parameter carrying this
/// attribute is to be used as field name.
/// </summary>
public string? FieldName { get; }
/// <summary>
/// <para>Gets or sets a value indicating whether to send a <c>400 Bad Request</c> response
/// to the client if the URL query contains no values for the field.</para>
/// <para>If this property is <see langword="true"/> and the URL query
/// contains no values for the field, the <c>400 Bad Request</c> response sent
/// to the client will contain a reference to the missing field.</para>
/// <para>If this property is <see langword="false"/> and the URL query
/// contains no values for the field, the default value for the parameter
/// (or a zero-length array if the parameter is of an array type)
/// will be passed to the controller method.</para>
/// </summary>
public bool BadRequestIfMissing { get; }
Task<string?> IRequestDataAttribute<WebApiController, string>.GetRequestDataAsync(
WebApiController controller,
string parameterName)
{
var data = controller.HttpContext.GetRequestQueryData();
var fieldName = FieldName ?? parameterName;
if (!data.ContainsKey(fieldName) && BadRequestIfMissing)
throw HttpException.BadRequest($"Missing query field {fieldName}.");
return Task.FromResult(data.GetValues(fieldName)?.LastOrDefault());
}
Task<string[]> IRequestDataAttribute<WebApiController, string[]>.GetRequestDataAsync(
WebApiController controller,
string parameterName)
{
var data = controller.HttpContext.GetRequestQueryData();
var fieldName = FieldName ?? parameterName;
if (!data.ContainsKey(fieldName) && BadRequestIfMissing)
throw HttpException.BadRequest($"Missing query field {fieldName}.");
return Task.FromResult(data.GetValues(fieldName) ?? Array.Empty<string>());
}
Task<object?> IRequestDataAttribute<WebApiController>.GetRequestDataAsync(
WebApiController controller,
Type type,
string parameterName)
{
var data = controller.HttpContext.GetRequestQueryData();
var fieldName = FieldName ?? parameterName;
if (!data.ContainsKey(fieldName) && BadRequestIfMissing)
throw HttpException.BadRequest($"Missing query field {fieldName}.");
if (type.IsArray)
{
var fieldValues = data.GetValues(fieldName) ?? Array.Empty<string>();
if (!FromString.TryConvertTo(type, fieldValues, out var result))
throw HttpException.BadRequest($"Cannot convert field {fieldName} to an array of {type.GetElementType().Name}.");
return Task.FromResult(result);
}
else
{
var fieldValue = data.GetValues(fieldName)?.LastOrDefault();
if (fieldValue == null)
return Task.FromResult(type.IsValueType ? Activator.CreateInstance(type) : null);
if (!FromString.TryConvertTo(type, fieldValue, out var result))
throw HttpException.BadRequest($"Cannot convert field {fieldName} to {type.Name}.");
return Task.FromResult(result);
}
}
}
}