Skip to content

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
merged 6 commits into from
Apr 14, 2020

Conversation

alefranz
Copy link
Contributor

@alefranz alefranz commented Jan 9, 2020

In the Model Validation, when MaxAllowedErrors is reached, the following models are pushed but not popped from the stack, raising incorrectly a InvalidOperationException if the number of remaining items is more then MaxValidationDepth.

Addresses #13778

Probably is better to look at the commits individually, as given I consolidated a test the diff is not optimal:
1- Consolidated two similar tests to a test theory
2 - Actual fix (later refactored) and related test case to reproduce the issue
3 - Refactored Visitor logic to reduce the risk of a missing Pop

/cc @ryanbrandenburg

@@ -441,8 +448,6 @@ public void Dispose()
_visitor.Metadata = _metadata;
_visitor.Model = _model;
_visitor.Strategy = _strategy;

_visitor._currentPath.Pop(_newModel);
Copy link
Member

Choose a reason for hiding this comment

The 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 👍

@Pilchie Pilchie added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jan 9, 2020
@alefranz
Copy link
Contributor Author

Hi @rynowak !
Should I rebase this?

@alefranz
Copy link
Contributor Author

alefranz commented Apr 7, 2020

Oops! didn't realize I forgot to update the reference assembly!

Copy link
Contributor

@ryanbrandenburg ryanbrandenburg left a comment

Choose a reason for hiding this comment

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

These changes seem fine to me. All the tests pass and it seems cleaner, so as long as @rynowak (who seems to have some existing knowledge in this area) is happy I'm ready to merge this.

@@ -3256,7 +3256,7 @@ public partial class ValidationVisitor
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public StateManager(Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor visitor, object newModel) { throw null; }
public StateManager(Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor visitor) { throw null; }
Copy link
Member

Choose a reason for hiding this comment

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

This is a breaking change to a public API (protected nested class of a public class). Can you add back the old constructor even if it's not used? We try to avoid API breaks when they are low value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, makes sense!

@rynowak
Copy link
Member

rynowak commented Apr 14, 2020

Sorry, I didn't realize y'all were waiting on me. We need to address this feedback and then this PR is ready.

@rynowak rynowak closed this Apr 14, 2020
@rynowak rynowak reopened this Apr 14, 2020
@rynowak
Copy link
Member

rynowak commented Apr 14, 2020

and then he clicks the wrong button as he apologizes....

@alefranz
Copy link
Contributor Author

😂 I often click it as well.
PR Updated! thanks!

@rynowak rynowak merged commit 970c924 into dotnet:master Apr 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants