Skip to content

Use ReadOnlyDictionary/ReadOnlyCollection for ValidateContext.ValidationErrors backing collections - #67822

Merged
Youssef1313 merged 4 commits into
mainfrom
copilot/consider-exposing-immutable-collections
Jul 29, 2026
Merged

Youssef1313 merged 4 commits into
mainfrom
copilot/consider-exposing-immutable-collections

Conversation

Copilot AI commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

ValidateContext.ValidationErrors was typed as IReadOnlyDictionary<string, IReadOnlyList<string>> but backed by an actual Dictionary<string, IReadOnlyList<string>> storing real List<string> values — allowing callers to cast back to the mutable types and externally corrupt validation state.

Description

  • ValidateContext: Split the single _validationErrors field into three:

    • _mutableValidationErrors (Dictionary<string, List<string>>) — internal accumulation
    • _readOnlyValidationErrors (Dictionary<string, IReadOnlyList<string>>) — parallel dict of ReadOnlyCollection<string> wrappers (live views via .AsReadOnly())
    • _validationErrorsView (ReadOnlyDictionary<string, IReadOnlyList<string>>) — the immutable wrapper returned from ValidationErrors
  • Removed the unsafe ((List<string>)existingErrors) cast from AddValidationErrorSuppressEvent

  • Test: Added ValidationErrors_ReturnsImmutableCollections verifying neither the outer dictionary nor the inner lists are castable to their mutable counterparts, and that mutation attempts throw NotSupportedException

  • You'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.

Copilot AI changed the title [WIP] Update ValidateContext to expose immutable collections for ValidationErrors Use ReadOnlyDictionary/ReadOnlyCollection for ValidateContext.ValidationErrors backing collections Jul 15, 2026
Copilot AI requested a review from Youssef1313 July 15, 2026 17:58
@Youssef1313

Copy link
Copy Markdown
Member

@ilonatommy FYI I see AsyncItemsProvider_DoesNotFlashPlaceholderRows_OnAnchoredEdgeGrowth failure here and looks unrelated to my change. Looks like you already attempted to fix it in #67679 but still failing?

@Youssef1313

Copy link
Copy Markdown
Member

/ba-g Blazor E2E failure looks unrelated

@Youssef1313
Youssef1313 marked this pull request as ready for review July 17, 2026 07:44
@Youssef1313
Youssef1313 requested review from Copilot and halter73 July 17, 2026 07:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ValidateContext to accumulate errors in internal mutable collections while exposing a ReadOnlyDictionary<string, IReadOnlyList<string>> view (with ReadOnlyCollection<string> values).
  • Removed the unsafe cast in AddValidationErrorSuppressEvent that previously assumed the stored value was a mutable List<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.

@ilonatommy

Copy link
Copy Markdown
Member

AsyncItemsProvider_DoesNotFlashPlaceholderRows_OnAnchoredEdgeGrowth

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.

@Youssef1313

Copy link
Copy Markdown
Member

/ba-g Failure is unrelated.

@Youssef1313 Youssef1313 added the * NO MERGE * Do not merge this PR as long as this label is present. label Jul 22, 2026
@Youssef1313

Copy link
Copy Markdown
Member

Doing a bigger refactoring in another PR and don't want conflicts in the other PR. Adding no-merge label for now.

@Youssef1313

Copy link
Copy Markdown
Member

@copilot Resolve the conflict here

@Youssef1313 Youssef1313 removed the * NO MERGE * Do not merge this PR as long as this label is present. label Jul 25, 2026
Copilot AI requested review from a team and wtgodbe as code owners July 25, 2026 09:23

Copilot AI commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Resolve the conflict here

Resolved in commit 671b5dd. The two conflicts were in ValidateContext.cs and ValidatableParameterInfoTests.cs, both caused by the localization refactor in main (#67987) which replaced the string-based error API with the new ValidationError type and simplified the class structure. The read-only wrapping approach has been adapted accordingly: ValidateContext now uses Dictionary<string, List<ValidationError>> internally and exposes a ReadOnlyDictionary<string, IReadOnlyList<ValidationError>> view, and the immutability test has been updated to use the new test helpers and ValidationError type.

@Youssef1313

Copy link
Copy Markdown
Member

@copilot Don't touch submodules

Copilot AI commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Don't touch submodules

Fixed in commit 315bfdae. A previous report_progress commit accidentally regressed the googletest submodule pointer (reverting it from a503186d back to 4141c384). Restored it to a503186d to match what the merge with main correctly brought in.

@Youssef1313
Youssef1313 force-pushed the copilot/consider-exposing-immutable-collections branch from 5a1f61c to 1e64758 Compare July 25, 2026 15:01

@rokonec rokonec left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rokonec rokonec Jul 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halter73 thoughts please?

@Youssef1313
Youssef1313 merged commit 5997c34 into main Jul 29, 2026
26 checks passed
@Youssef1313
Youssef1313 deleted the copilot/consider-exposing-immutable-collections branch July 29, 2026 11:15
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Jul 30, 2026
@Youssef1313 Youssef1313 added the feature-validation Issues related to model validation in minimal and controller-based APIs label Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature-validation Issues related to model validation in minimal and controller-based APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider exposing immutable backing collections for ValidateContext.ValidationErrors

6 participants