Repository navigation
Use ReadOnlyDictionary/ReadOnlyCollection for ValidateContext.ValidationErrors backing collections - #67822
Conversation
|
@ilonatommy FYI I see |
|
/ba-g Blazor E2E failure looks unrelated |
There was a problem hiding this comment.
Pull request overview
This PR hardens the ValidateContext.ValidationErrors public surface by ensuring it’s backed by genuinely read-only collection wrappers, preventing callers from casting back to mutable Dictionary/List instances and mutating validation state externally.
Changes:
- Reworked
ValidateContextto accumulate errors in internal mutable collections while exposing aReadOnlyDictionary<string, IReadOnlyList<string>>view (withReadOnlyCollection<string>values). - Removed the unsafe cast in
AddValidationErrorSuppressEventthat previously assumed the stored value was a mutableList<string>. - Added a unit test asserting the returned collections are not mutable/castable and that mutation attempts throw
NotSupportedException.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Validation/src/ValidateContext.cs | Splits error storage into mutable accumulation + read-only views to prevent external mutation via casts. |
| src/Validation/test/Microsoft.Extensions.Validation.Tests/ValidatableParameterInfoTests.cs | Adds coverage to ensure ValidationErrors returns immutable/read-only collections and rejects mutation attempts. |
It's a new test, added in #67679. I didn't hit any failures neither locally nor on CI but good to know. I will investigate it. |
|
/ba-g Failure is unrelated. |
|
Doing a bigger refactoring in another PR and don't want conflicts in the other PR. Adding no-merge label for now. |
|
@copilot Resolve the conflict here |
Resolved in commit |
|
@copilot Don't touch submodules |
Fixed in commit |
5a1f61c to
1e64758
Compare
…ionErrors backing collections
1e64758 to
a1894ed
Compare
rokonec
left a comment
There was a problem hiding this comment.
Functionally it is sound, but I would like to open discussion for this change necessity.
| @@ -44,7 +46,7 @@ public ValidateContext() | |||
| /// There are no guarantees whether or not this dictionary is lazy. Usages should treat null and empty dictionary the same. | |||
| /// </remarks> | |||
| public IReadOnlyDictionary<string, IReadOnlyList<ValidationError>>? ValidationErrors | |||
There was a problem hiding this comment.
Did we considered runtime performance?
I feel like we might be harming everyone to avoid mistakes of few. With my limited knowledge of usage patterns I would like to be sure that it was considered.
Good intent explicit cast to Dictionary will be, IMO, very rare bug.
There was a problem hiding this comment.
The dictionary is only ever created when there are validation errors. I don't think this single extra allocation for this rare scenario is going to make any meaningful difference.
There was a problem hiding this comment.
This is good point. It however expect that happy/common path will not have validation errors. The fact that context.ValidationErrors?.Empty() could be more expensive that intuitively expected can lead to subtle performance bugs.
I personally believe that defense against "explicit cast + mutation" is not worth additional code complexity and performance surprises risks.
But it is not a strong opinion in this particular case, please feel free to resolve this conversation.
ValidateContext.ValidationErrorswas typed asIReadOnlyDictionary<string, IReadOnlyList<string>>but backed by an actualDictionary<string, IReadOnlyList<string>>storing realList<string>values — allowing callers to cast back to the mutable types and externally corrupt validation state.Description
ValidateContext: Split the single_validationErrorsfield into three:_mutableValidationErrors(Dictionary<string, List<string>>) — internal accumulation_readOnlyValidationErrors(Dictionary<string, IReadOnlyList<string>>) — parallel dict ofReadOnlyCollection<string>wrappers (live views via.AsReadOnly())_validationErrorsView(ReadOnlyDictionary<string, IReadOnlyList<string>>) — the immutable wrapper returned fromValidationErrorsRemoved the unsafe
((List<string>)existingErrors)cast fromAddValidationErrorSuppressEventTest: Added
ValidationErrors_ReturnsImmutableCollectionsverifying neither the outer dictionary nor the inner lists are castable to their mutable counterparts, and that mutation attempts throwNotSupportedExceptionYou've read the Contributor Guide and Code of Conduct.
You've included unit or integration tests for your change, where applicable.
You've included inline docs for your change, where applicable.
There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.