question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with replacing IObjectModelValidator

See original GitHub issue

Hi,

I followed your example on disabling validation (see: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0) and noticed some strange side-behavior, e.g. model validation on classes was indeed disabled, but validation on action (method) parameters (f.e. passed by query) resulted in a bad request (HTTP 400)

In short: a POST with a model in the body functioned as documented, but a GET with query parameters didn’t.

the example code tested:

public class NullObjectModelValidator : IObjectModelValidator
{
    public void Validate(ActionContext actionContext,
        ValidationStateDictionary validationState, string prefix, object model)
    {

    }
}

… and …

services.AddSingleton<IObjectModelValidator, NullObjectModelValidator>();

Is this by design or indeed a bug?

TIA

Bert

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MaceWinducommented, May 21, 2021

@amoorthaemer , example in documentation is not correct, as it leaves model in unvalidated state, which doesn’t work at least with [FromBody] and [FromHeaders] parameters.

Validate body should look like that:

void IObjectModelValidator.Validate(ActionContext actionContext, ValidationStateDictionary validationState, string prefix, object model)
{
    foreach (var entry in actionContext.ModelState.Values)
    {
        // or ModelValidationState.Valid
        entry.ValidationState = ModelValidationState.Skipped;
    }
}
0reactions
pranavkmcommented, Oct 4, 2021

Thanks, I think @MaceWindu’s response should address this. I’ve removed the section from our doc article, it’s not something we would recommend for most users, so I’ll consider this issue resolved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to register a custom IObjectModelValidator with The ...
1 Answer. I found the error by debugging the source of Asp.net MVC core, i was using the method tryAddSingleton instead of AddSingleton...
Read more >
ASP.NET Core breaking changes for versions 3.0 and 3.1
The MemoryCacheOptions.CompactOnMemoryPressure property has been removed. Reason for change. Automatically compacting the cache caused problems.
Read more >
Breaking changes in .NET Core 3.0
The MemoryCacheOptions.CompactOnMemoryPressure property has been removed. Reason for change. Automatically compacting the cache caused problems.
Read more >
Dependency Injection - ASP.NET Core Documentation
NET Core provides a minimal feature set and is not intended to replace other containers. Sections: What is Dependency Injection?
Read more >
Asp.net, MVC, Silverlight, WPF, WCF, WF - Page 3
Namely, as soon as model binding returns an objects tree an implementation of the “IObjectModelValidator” interface is retrieved through dependency injection ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found