forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutableObjectModelBinder.cs
More file actions
325 lines (281 loc) · 15.4 KB
/
Copy pathMutableObjectModelBinder.cs
File metadata and controls
325 lines (281 loc) · 15.4 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
// 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.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Web.Http.Controllers;
using System.Web.Http.Internal;
using System.Web.Http.Metadata;
using System.Web.Http.Properties;
using System.Web.Http.Validation;
namespace System.Web.Http.ModelBinding.Binders
{
public class MutableObjectModelBinder : IModelBinder
{
internal ModelMetadataProvider MetadataProvider { private get; set; }
internal static bool CanBindType(Type modelType)
{
// Simple types cannot use this binder
bool isComplexType = !TypeHelper.HasStringConverter(modelType);
if (!isComplexType)
{
return false;
}
if (modelType == typeof(ComplexModelDto))
{
// forbidden type - will cause a stack overflow if we try binding this type
return false;
}
return true;
}
public virtual bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
ModelBindingHelper.ValidateBindingContext(bindingContext);
if (!bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
{
// no values to bind
return false;
}
if (!CanBindType(bindingContext.ModelType))
{
return false;
}
EnsureModel(actionContext, bindingContext);
IEnumerable<ModelMetadata> propertyMetadatas = GetMetadataForProperties(actionContext, bindingContext);
ComplexModelDto dto = CreateAndPopulateDto(actionContext, bindingContext, propertyMetadatas);
// post-processing, e.g. property setters and hooking up validation
ProcessDto(actionContext, bindingContext, dto);
bindingContext.ValidationNode.ValidateAllProperties = true; // complex models require full validation
return true;
}
protected virtual bool CanUpdateProperty(ModelMetadata propertyMetadata)
{
return CanUpdatePropertyInternal(propertyMetadata);
}
internal static bool CanUpdatePropertyInternal(ModelMetadata propertyMetadata)
{
return !propertyMetadata.IsReadOnly || CanUpdateReadOnlyProperty(propertyMetadata.ModelType);
}
private static bool CanUpdateReadOnlyProperty(Type propertyType)
{
// Value types have copy-by-value semantics, which prevents us from updating
// properties that are marked readonly.
if (propertyType.IsValueType)
{
return false;
}
// Arrays are strange beasts since their contents are mutable but their sizes aren't.
// Therefore we shouldn't even try to update these. Further reading:
// http://blogs.msdn.com/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx
if (propertyType.IsArray)
{
return false;
}
// Special-case known immutable reference types
if (propertyType == typeof(string))
{
return false;
}
return true;
}
private ComplexModelDto CreateAndPopulateDto(HttpActionContext actionContext, ModelBindingContext bindingContext, IEnumerable<ModelMetadata> propertyMetadatas)
{
ModelMetadataProvider metadataProvider = MetadataProvider ?? actionContext.GetMetadataProvider();
// create a DTO and call into the DTO binder
ComplexModelDto originalDto = new ComplexModelDto(bindingContext.ModelMetadata, propertyMetadatas);
ModelBindingContext dtoBindingContext = new ModelBindingContext(bindingContext)
{
ModelMetadata = metadataProvider.GetMetadataForType(() => originalDto, typeof(ComplexModelDto)),
ModelName = bindingContext.ModelName
};
actionContext.Bind(dtoBindingContext);
return (ComplexModelDto)dtoBindingContext.Model;
}
protected virtual object CreateModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
// If the Activator throws an exception, we want to propagate it back up the call stack, since the application
// developer should know that this was an invalid type to try to bind to.
return Activator.CreateInstance(bindingContext.ModelType);
}
// Called when the property setter null check failed, allows us to add our own error message to ModelState.
internal static EventHandler<ModelValidatedEventArgs> CreateNullCheckFailedHandler(ModelMetadata modelMetadata, object incomingValue)
{
return (sender, e) =>
{
ModelValidationNode validationNode = (ModelValidationNode)sender;
ModelStateDictionary modelState = e.ActionContext.ModelState;
if (modelState.IsValidField(validationNode.ModelStateKey))
{
string errorMessage = ModelBinderConfig.ValueRequiredErrorMessageProvider(e.ActionContext, modelMetadata, incomingValue);
if (errorMessage != null)
{
modelState.AddModelError(validationNode.ModelStateKey, errorMessage);
}
}
};
}
protected virtual void EnsureModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
if (bindingContext.Model == null)
{
bindingContext.ModelMetadata.Model = CreateModel(actionContext, bindingContext);
}
}
protected virtual IEnumerable<ModelMetadata> GetMetadataForProperties(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
// keep a set of the required properties so that we can cross-reference bound properties later
HashSet<string> requiredProperties;
Dictionary<string, ModelValidator> requiredValidators;
HashSet<string> skipProperties;
GetRequiredPropertiesCollection(actionContext, bindingContext, out requiredProperties, out requiredValidators, out skipProperties);
return from propertyMetadata in bindingContext.ModelMetadata.Properties
let propertyName = propertyMetadata.PropertyName
let shouldUpdateProperty = requiredProperties.Contains(propertyName) || !skipProperties.Contains(propertyName)
where shouldUpdateProperty && CanUpdateProperty(propertyMetadata)
select propertyMetadata;
}
private static object GetPropertyDefaultValue(PropertyDescriptor propertyDescriptor)
{
DefaultValueAttribute attr = propertyDescriptor.Attributes.OfType<DefaultValueAttribute>().FirstOrDefault();
return (attr != null) ? attr.Value : null;
}
internal static void GetRequiredPropertiesCollection(HttpActionContext actionContext, ModelBindingContext bindingContext, out HashSet<string> requiredProperties, out Dictionary<string, ModelValidator> requiredValidators, out HashSet<string> skipProperties)
{
requiredProperties = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
requiredValidators = new Dictionary<string, ModelValidator>(StringComparer.OrdinalIgnoreCase);
skipProperties = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
// Use attributes on the property before attributes on the type.
ICustomTypeDescriptor modelDescriptor = TypeDescriptorHelper.Get(bindingContext.ModelType);
PropertyDescriptorCollection propertyDescriptors = modelDescriptor.GetProperties();
HttpBindingBehaviorAttribute typeAttr = modelDescriptor.GetAttributes().OfType<HttpBindingBehaviorAttribute>().SingleOrDefault();
foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors)
{
string propertyName = propertyDescriptor.Name;
ModelMetadata propertyMetadata = bindingContext.PropertyMetadata[propertyName];
ModelValidator requiredValidator = actionContext.GetValidators(propertyMetadata).Where(v => v.IsRequired).FirstOrDefault();
requiredValidators[propertyName] = requiredValidator;
HttpBindingBehaviorAttribute propAttr = propertyDescriptor.Attributes.OfType<HttpBindingBehaviorAttribute>().SingleOrDefault();
HttpBindingBehaviorAttribute workingAttr = propAttr ?? typeAttr;
if (workingAttr != null)
{
switch (workingAttr.Behavior)
{
case HttpBindingBehavior.Required:
requiredProperties.Add(propertyName);
break;
case HttpBindingBehavior.Never:
skipProperties.Add(propertyName);
break;
}
}
else if (requiredValidator != null)
{
requiredProperties.Add(propertyName);
}
}
}
internal void ProcessDto(HttpActionContext actionContext, ModelBindingContext bindingContext, ComplexModelDto dto)
{
HashSet<string> requiredProperties;
Dictionary<string, ModelValidator> requiredValidators;
HashSet<string> skipProperties;
GetRequiredPropertiesCollection(actionContext, bindingContext, out requiredProperties, out requiredValidators, out skipProperties);
// Eliminate provided properties from requiredProperties; leaving just *missing* required properties.
requiredProperties.ExceptWith(dto.Results.Select(r => r.Key.PropertyName));
foreach (string missingRequiredProperty in requiredProperties)
{
string modelStateKey = ModelBindingHelper.CreatePropertyModelName(
bindingContext.ValidationNode.ModelStateKey, missingRequiredProperty);
// Update Model as SetProperty() would: Place null value where validator will check for non-null. This
// ensures a failure result from a required validator (if any) even for a non-nullable property.
// (Otherwise, propertyMetadata.Model is likely already null.)
ModelMetadata propertyMetadata = bindingContext.PropertyMetadata[missingRequiredProperty];
propertyMetadata.Model = null;
// Execute validator (if any) to get custom error message.
ModelValidator validator = requiredValidators[missingRequiredProperty];
bool addedError = RunValidator(validator, bindingContext, propertyMetadata, modelStateKey);
// Fall back to default message if HttpBindingBehaviorAttribute required this property or validator
// (oddly) succeeded.
if (!addedError)
{
bindingContext.ModelState.AddModelError(modelStateKey,
Error.Format(SRResources.MissingRequiredMember, missingRequiredProperty));
}
}
// for each property that was bound, call the setter, recording exceptions as necessary
foreach (var entry in dto.Results)
{
ModelMetadata propertyMetadata = entry.Key;
ComplexModelDtoResult dtoResult = entry.Value;
if (dtoResult != null)
{
SetProperty(actionContext, bindingContext, propertyMetadata, dtoResult, requiredValidators[propertyMetadata.PropertyName]);
bindingContext.ValidationNode.ChildNodes.Add(dtoResult.ValidationNode);
}
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We're recording this exception so that we can act on it later.")]
protected virtual void SetProperty(HttpActionContext actionContext, ModelBindingContext bindingContext, ModelMetadata propertyMetadata, ComplexModelDtoResult dtoResult, ModelValidator requiredValidator)
{
PropertyDescriptor propertyDescriptor = TypeDescriptorHelper.Get(bindingContext.ModelType).GetProperties().Find(propertyMetadata.PropertyName, true /* ignoreCase */);
if (propertyDescriptor == null || propertyDescriptor.IsReadOnly)
{
return; // nothing to do
}
object value = dtoResult.Model ?? GetPropertyDefaultValue(propertyDescriptor);
propertyMetadata.Model = value;
// 'Required' validators need to run first so that we can provide useful error messages if
// the property setters throw, e.g. if we're setting entity keys to null. See comments in
// DefaultModelBinder.SetProperty() for more information.
if (value == null)
{
string modelStateKey = dtoResult.ValidationNode.ModelStateKey;
if (bindingContext.ModelState.IsValidField(modelStateKey))
{
RunValidator(requiredValidator, bindingContext, propertyMetadata, modelStateKey);
}
}
if (value != null || TypeHelper.TypeAllowsNullValue(propertyDescriptor.PropertyType))
{
try
{
propertyDescriptor.SetValue(bindingContext.Model, value);
}
catch (Exception ex)
{
// don't display a duplicate error message if a binding error has already occurred for this field
string modelStateKey = dtoResult.ValidationNode.ModelStateKey;
if (bindingContext.ModelState.IsValidField(modelStateKey))
{
bindingContext.ModelState.AddModelError(modelStateKey, ex);
}
}
}
else
{
// trying to set a non-nullable value type to null, need to make sure there's a message
string modelStateKey = dtoResult.ValidationNode.ModelStateKey;
if (bindingContext.ModelState.IsValidField(modelStateKey))
{
dtoResult.ValidationNode.Validated += CreateNullCheckFailedHandler(propertyMetadata, value);
}
}
}
// Returns true if validator execution adds a model error.
private static bool RunValidator(ModelValidator validator, ModelBindingContext bindingContext,
ModelMetadata propertyMetadata, string modelStateKey)
{
bool addedError = false;
if (validator != null)
{
foreach (ModelValidationResult validationResult in validator.Validate(propertyMetadata, bindingContext.Model))
{
bindingContext.ModelState.AddModelError(modelStateKey, validationResult.Message);
addedError = true;
}
}
return addedError;
}
}
}