Skip to content

Commit 15169a5

Browse files
committed
Fix CA2227, "Collection properties should be read only" warnings (as errors)
- follow up to eebbf38
1 parent dca1770 commit 15169a5

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/System.Web.Http/Validation/BodyModelValidatorContext.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ namespace System.Web.Http.Validation
1313
/// </summary>
1414
public class BodyModelValidatorContext
1515
{
16+
public BodyModelValidatorContext(ModelStateDictionary modelState)
17+
{
18+
if (modelState == null)
19+
{
20+
throw new ArgumentNullException("modelState");
21+
}
22+
23+
KeyBuilders = new Stack<IBodyModelValidatorKeyBuilder>();
24+
ModelState = modelState;
25+
Visited = new HashSet<object>(ReferenceEqualityComparer.Instance);
26+
}
27+
1628
/// <summary>
1729
/// Gets or sets the <see cref="ModelMetadataProvider"/> used to provide the model metadata.
1830
/// </summary>
@@ -29,21 +41,21 @@ public class BodyModelValidatorContext
2941
public IModelValidatorCache ValidatorCache { get; set; }
3042

3143
/// <summary>
32-
/// Gets or sets the current <see cref="ModelStateDictionary"/>.
44+
/// Gets the current <see cref="ModelStateDictionary"/>.
3345
/// </summary>
34-
public ModelStateDictionary ModelState { get; set; }
46+
public ModelStateDictionary ModelState { get; private set; }
3547

3648
/// <summary>
37-
/// Gets or sets the set of model objects visited in this validation. Includes the model being validated in the
49+
/// Gets the set of model objects visited in this validation. Includes the model being validated in the
3850
/// current scope.
3951
/// </summary>
40-
public HashSet<object> Visited { get; set; }
52+
public HashSet<object> Visited { get; private set; }
4153

4254
/// <summary>
43-
/// Gets or sets the stack of <see cref="IBodyModelValidatorKeyBuilder"/>s used in this validation. Includes
55+
/// Gets the stack of <see cref="IBodyModelValidatorKeyBuilder"/>s used in this validation. Includes
4456
/// the <see cref="IBodyModelValidatorKeyBuilder"/> to generate model state keys for the current scope.
4557
/// </summary>
46-
public Stack<IBodyModelValidatorKeyBuilder> KeyBuilders { get; set; }
58+
public Stack<IBodyModelValidatorKeyBuilder> KeyBuilders { get; private set; }
4759

4860
/// <summary>
4961
/// Gets or sets the model state prefix for the root scope of this validation.

src/System.Web.Http/Validation/DefaultBodyModelValidator.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,11 @@ public bool Validate(object model, Type type, ModelMetadataProvider metadataProv
5959
}
6060

6161
ModelMetadata metadata = metadataProvider.GetMetadataForType(() => model, type);
62-
BodyModelValidatorContext validationContext = new BodyModelValidatorContext
62+
BodyModelValidatorContext validationContext = new BodyModelValidatorContext(actionContext.ModelState)
6363
{
6464
MetadataProvider = metadataProvider,
6565
ActionContext = actionContext,
6666
ValidatorCache = actionContext.GetValidatorCache(),
67-
ModelState = actionContext.ModelState,
68-
Visited = new HashSet<object>(ReferenceEqualityComparer.Instance),
69-
KeyBuilders = new Stack<IBodyModelValidatorKeyBuilder>(),
7067
RootPrefix = keyPrefix
7168
};
7269
return ValidateNodeAndChildren(metadata, validationContext, container: null, validators: null);

0 commit comments

Comments
 (0)