-
Notifications
You must be signed in to change notification settings - Fork 10.4k
ModelValidation: Fixed MaxDepth exception when there are more items left then MaxValidationDepth after reaching MaxAllowedErrors #18212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rynowak
merged 6 commits into
dotnet:master
from
alefranz:modelvalidation-maxerrors-doesnotpop
Apr 14, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
87e0c50
Consolidated similar tests in a theory
alefranz 870a5aa
Fix for InvalidOperationException when reached maxAllowedErrors and t…
alefranz e15d3c3
Refactored Visitor logic to reduce the risk of a missing Pop.
alefranz e3b8a30
Added comment on when it can throw
alefranz eee2460
Updated reference assembly
alefranz 1a58f48
Reverted publid API change
alefranz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,13 +223,25 @@ protected virtual bool Visit(ModelMetadata metadata, string key, object model) | |
return true; | ||
} | ||
|
||
bool result; | ||
try | ||
{ | ||
// Throws InvalidOperationException if the object graph is too deep | ||
result = VisitImplementation(ref metadata, ref key, model); | ||
} | ||
finally | ||
{ | ||
_currentPath.Pop(model); | ||
} | ||
return result; | ||
} | ||
|
||
private bool VisitImplementation(ref ModelMetadata metadata, ref string key, object model) | ||
{ | ||
if (MaxValidationDepth != null && _currentPath.Count > MaxValidationDepth) | ||
{ | ||
// Non cyclic but too deep an object graph. | ||
|
||
// Pop the current model to make ValidationStack.Dispose happy | ||
_currentPath.Pop(model); | ||
|
||
string message; | ||
switch (metadata.MetadataKind) | ||
{ | ||
|
@@ -264,7 +276,6 @@ protected virtual bool Visit(ModelMetadata metadata, string key, object model) | |
{ | ||
// Use the key on the entry, because we might not have entries in model state. | ||
SuppressValidation(entry.Key); | ||
_currentPath.Pop(model); | ||
return true; | ||
} | ||
// If the metadata indicates that no validators exist AND the aggregate state for the key says that the model graph | ||
|
@@ -282,7 +293,6 @@ protected virtual bool Visit(ModelMetadata metadata, string key, object model) | |
} | ||
} | ||
|
||
_currentPath.Pop(model); | ||
return true; | ||
} | ||
|
||
|
@@ -401,7 +411,6 @@ protected virtual ValidationStateEntry GetValidationEntry(object model) | |
private readonly string _key; | ||
private readonly ModelMetadata _metadata; | ||
private readonly object _model; | ||
private readonly object _newModel; | ||
private readonly IValidationStrategy _strategy; | ||
|
||
public static StateManager Recurse( | ||
|
@@ -411,7 +420,7 @@ public static StateManager Recurse( | |
object model, | ||
IValidationStrategy strategy) | ||
{ | ||
var recursifier = new StateManager(visitor, model); | ||
var recursifier = new StateManager(visitor, null); | ||
|
||
visitor.Container = visitor.Model; | ||
visitor.Key = key; | ||
|
@@ -425,7 +434,6 @@ public static StateManager Recurse( | |
public StateManager(ValidationVisitor visitor, object newModel) | ||
{ | ||
_visitor = visitor; | ||
_newModel = newModel; | ||
|
||
_container = _visitor.Container; | ||
_key = _visitor.Key; | ||
|
@@ -441,8 +449,6 @@ public void Dispose() | |
_visitor.Metadata = _metadata; | ||
_visitor.Model = _model; | ||
_visitor.Strategy = _strategy; | ||
|
||
_visitor._currentPath.Pop(_newModel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part seems like a big red flag (in the existing code, not in your changes). Seeing pushes and pops so disconnected from each other is a good clue that we had a bug here, your changes make this much easier to reason about 👍 |
||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.