forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultBodyModelValidator.cs
More file actions
362 lines (323 loc) · 15.1 KB
/
Copy pathDefaultBodyModelValidator.cs
File metadata and controls
362 lines (323 loc) · 15.1 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net.Http.Formatting;
using System.Runtime.CompilerServices;
using System.Web.Http.Controllers;
using System.Web.Http.Internal;
using System.Web.Http.Metadata;
using System.Web.Http.ModelBinding;
namespace System.Web.Http.Validation
{
/// <summary>
/// Recursively validate an object.
/// </summary>
public class DefaultBodyModelValidator : IBodyModelValidator
{
/// <summary>
/// Determines whether the <paramref name="model"/> is valid and adds any validation errors to the <paramref name="actionContext"/>'s <see cref="ModelStateDictionary"/>
/// </summary>
/// <param name="model">The model to be validated.</param>
/// <param name="type">The <see cref="Type"/> to use for validation.</param>
/// <param name="metadataProvider">The <see cref="ModelMetadataProvider"/> used to provide the model metadata.</param>
/// <param name="actionContext">The <see cref="HttpActionContext"/> within which the model is being validated.</param>
/// <param name="keyPrefix">The <see cref="string"/> to append to the key for any validation errors.</param>
/// <returns><c>true</c>if <paramref name="model"/> is valid, <c>false</c> otherwise.</returns>
public bool Validate(object model, Type type, ModelMetadataProvider metadataProvider, HttpActionContext actionContext, string keyPrefix)
{
if (type == null)
{
throw Error.ArgumentNull("type");
}
if (metadataProvider == null)
{
throw Error.ArgumentNull("metadataProvider");
}
if (actionContext == null)
{
throw Error.ArgumentNull("actionContext");
}
if (model != null && !ShouldValidateType(model.GetType()))
{
return true;
}
ModelValidatorProvider[] validatorProviders = actionContext.GetValidatorProviders().ToArray();
// Optimization : avoid validating the object graph if there are no validator providers
if (validatorProviders == null || validatorProviders.Length == 0)
{
return true;
}
ModelMetadata metadata = metadataProvider.GetMetadataForType(() => model, type);
BodyModelValidatorContext validationContext = new BodyModelValidatorContext(actionContext.ModelState)
{
MetadataProvider = metadataProvider,
ActionContext = actionContext,
ValidatorCache = actionContext.GetValidatorCache(),
RootPrefix = keyPrefix
};
return ValidateNodeAndChildren(metadata, validationContext, container: null, validators: null);
}
/// <summary>
/// Determines whether instances of a particular type should be validated
/// </summary>
/// <param name="type">The type to validate.</param>
/// <returns><c>true</c> if the type should be validated; <c>false</c> otherwise</returns>
public virtual bool ShouldValidateType(Type type)
{
return !MediaTypeFormatterCollection.IsTypeExcludedFromValidation(type);
}
/// <summary>
/// Recursively validate the given <paramref name="metadata"/> and <paramref name="container"/>.
/// </summary>
/// <param name="metadata">The <see cref="ModelMetadata"/> for the object to validate.</param>
/// <param name="validationContext">The <see cref="BodyModelValidatorContext"/>.</param>
/// <param name="container">The object containing the object to validate.</param>
/// <param name="validators">The collection of <see cref="ModelValidator"/>s.</param>
/// <returns>
/// <see langword="true"/> if validation succeeds for the given <paramref name="metadata"/>,
/// <paramref name="container"/>, and child nodes; <see langword="false"/> otherwise.
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "See comment below")]
protected virtual bool ValidateNodeAndChildren(
ModelMetadata metadata,
BodyModelValidatorContext validationContext,
object container,
IEnumerable<ModelValidator> validators)
{
// Recursion guard to avoid stack overflows
RuntimeHelpers.EnsureSufficientExecutionStack();
if (metadata == null)
{
throw Error.ArgumentNull("metadata");
}
if (validationContext == null)
{
throw Error.ArgumentNull("validationContext");
}
object model = null;
try
{
model = metadata.Model;
}
catch
{
// Retrieving the model failed - typically caused by a property getter throwing
// Being unable to retrieve a property is not a validation error - many properties can only be retrieved if certain conditions are met
// For example, Uri.AbsoluteUri throws for relative URIs but it shouldn't be considered a validation error
return true;
}
bool isValid = true;
if (validators == null)
{
validators = validationContext.ActionContext.GetValidators(metadata, validationContext.ValidatorCache);
}
// We don't need to recursively traverse the graph for null values
if (model == null)
{
return ShallowValidate(metadata, validationContext, container, validators);
}
// We don't need to recursively traverse the graph for types that shouldn't be validated
Type modelType = model.GetType();
if (TypeHelper.IsSimpleType(modelType) || !ShouldValidateType(modelType))
{
return ShallowValidate(metadata, validationContext, container, validators);
}
// Check to avoid infinite recursion. This can happen with cycles in an object graph.
if (validationContext.Visited.Contains(model))
{
return true;
}
validationContext.Visited.Add(model);
// Validate the children first - depth-first traversal
IEnumerable enumerableModel = model as IEnumerable;
if (enumerableModel == null)
{
isValid = ValidateProperties(metadata, validationContext);
}
else
{
isValid = ValidateElements(enumerableModel, validationContext);
}
if (isValid)
{
// Don't bother to validate this node if children failed.
isValid = ShallowValidate(metadata, validationContext, container, validators);
}
// Pop the object so that it can be validated again in a different path
validationContext.Visited.Remove(model);
return isValid;
}
/// <summary>
/// Recursively validate the properties of the given <paramref name="metadata"/>.
/// </summary>
/// <param name="metadata">The <see cref="ModelMetadata"/> for the object to validate.</param>
/// <param name="validationContext">The <see cref="BodyModelValidatorContext"/>.</param>
/// <returns>
/// <see langword="true"/> if validation succeeds for all properties in <paramref name="metadata"/>;
/// <see langword="false"/> otherwise.
/// </returns>
protected virtual bool ValidateProperties(ModelMetadata metadata, BodyModelValidatorContext validationContext)
{
if (metadata == null)
{
throw Error.ArgumentNull("metadata");
}
if (validationContext == null)
{
throw Error.ArgumentNull("validationContext");
}
bool isValid = true;
PropertyScope propertyScope = new PropertyScope();
validationContext.KeyBuilders.Push(propertyScope);
foreach (ModelMetadata childMetadata in validationContext.MetadataProvider.GetMetadataForProperties(metadata.Model, metadata.RealModelType))
{
propertyScope.PropertyName = childMetadata.PropertyName;
if (!ValidateNodeAndChildren(childMetadata, validationContext, metadata.Model, validators: null))
{
isValid = false;
}
}
validationContext.KeyBuilders.Pop();
return isValid;
}
/// <summary>
/// Recursively validate the elements of the <paramref name="model"/> collection.
/// </summary>
/// <param name="model">The <see cref="IEnumerable"/> instance containing the elements to validate.</param>
/// <param name="validationContext">The <see cref="BodyModelValidatorContext"/>.</param>
/// <returns>
/// <see langword="true"/> if validation succeeds for all elements of <paramref name="model"/>;
/// <see langword="false"/> otherwise.
/// </returns>
protected virtual bool ValidateElements(IEnumerable model, BodyModelValidatorContext validationContext)
{
if (model == null)
{
throw Error.ArgumentNull("model");
}
if (validationContext == null)
{
throw Error.ArgumentNull("validationContext");
}
bool isValid = true;
Type elementType = GetElementType(model.GetType());
ModelMetadata elementMetadata = validationContext.MetadataProvider.GetMetadataForType(null, elementType);
ElementScope elementScope = new ElementScope() { Index = 0 };
validationContext.KeyBuilders.Push(elementScope);
IEnumerable<ModelValidator> validators = validationContext.ActionContext.GetValidators(elementMetadata, validationContext.ValidatorCache);
// if there are no validators or the object is null we bail out quickly
// when there are large arrays of null, this will save a significant amount of processing
// with minimal impact to other scenarios.
bool anyValidatorsDefined = validators.Any();
foreach (object element in model)
{
// If the element is non null, the recursive calls might find more validators.
// If it's null, then a shallow validation will be performed.
if (element != null || anyValidatorsDefined)
{
elementMetadata.Model = element;
if (!ValidateNodeAndChildren(elementMetadata, validationContext, model, validators))
{
isValid = false;
}
}
elementScope.Index++;
}
validationContext.KeyBuilders.Pop();
return isValid;
}
/// <summary>
/// Validate a single node, not including its children.
/// </summary>
/// <param name="metadata">The <see cref="ModelMetadata"/>.</param>
/// <param name="validationContext">The <see cref="BodyModelValidatorContext"/>.</param>
/// <param name="container">The object to validate.</param>
/// <param name="validators">The collection of <see cref="ModelValidator"/>s.</param>
/// <returns>
/// <see langword="true"/> if validation succeeds for the given <paramref name="metadata"/> and
/// <paramref name="container"/>; <see langword="false"/> otherwise.
/// </returns>
protected virtual bool ShallowValidate(
ModelMetadata metadata,
BodyModelValidatorContext validationContext,
object container,
IEnumerable<ModelValidator> validators)
{
if (metadata == null)
{
throw Error.ArgumentNull("metadata");
}
if (validationContext == null)
{
throw Error.ArgumentNull("validationContext");
}
if (validators == null)
{
throw Error.ArgumentNull("validators");
}
bool isValid = true;
string modelKey = null;
// When the are no validators we bail quickly. This saves a GetEnumerator allocation.
// In a large array (tens of thousands or more) scenario it's very significant.
ICollection validatorsAsCollection = validators as ICollection;
if (validatorsAsCollection != null && validatorsAsCollection.Count == 0)
{
return isValid;
}
foreach (ModelValidator validator in validators)
{
foreach (ModelValidationResult error in validator.Validate(metadata, container))
{
if (modelKey == null)
{
modelKey = validationContext.RootPrefix;
foreach (IBodyModelValidatorKeyBuilder keyBuilder in validationContext.KeyBuilders.Reverse())
{
modelKey = keyBuilder.AppendTo(modelKey);
}
}
string errorKey = ModelBindingHelper.CreatePropertyModelName(modelKey, error.MemberName);
validationContext.ModelState.AddModelError(errorKey, error.Message);
isValid = false;
}
}
return isValid;
}
private static Type GetElementType(Type type)
{
Contract.Assert(typeof(IEnumerable).IsAssignableFrom(type));
if (type.IsArray)
{
return type.GetElementType();
}
foreach (Type implementedInterface in type.GetInterfaces())
{
if (implementedInterface.IsGenericType && implementedInterface.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
return implementedInterface.GetGenericArguments()[0];
}
}
return typeof(object);
}
private class PropertyScope : IBodyModelValidatorKeyBuilder
{
public string PropertyName { get; set; }
public string AppendTo(string prefix)
{
return ModelBindingHelper.CreatePropertyModelName(prefix, PropertyName);
}
}
private class ElementScope : IBodyModelValidatorKeyBuilder
{
public int Index { get; set; }
public string AppendTo(string prefix)
{
return ModelBindingHelper.CreateIndexModelName(prefix, Index);
}
}
}
}