Skip to content
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

Better Error Message in ControllerBase.ValidationProblem #15166

Closed
bradlis7 opened this issue Oct 18, 2019 · 4 comments · Fixed by #24985
Closed

Better Error Message in ControllerBase.ValidationProblem #15166

bradlis7 opened this issue Oct 18, 2019 · 4 comments · Fixed by #24985
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Done This issue has been fixed help wanted Up for grabs. We would accept a PR to help resolve this issue task
Milestone

Comments

@bradlis7
Copy link

Describe the bug

During Unit Tests, I was getting a NullReferenceExeption in ControllerBase.ValidationProblem. Difficult to narrow down the problem.

To Reproduce

Steps to reproduce the behavior:

  1. 3.0.0
  2. Create a controller unit test of an action that calls ValidationResult.
var c  = new MyController();
c.MyAction(); // Should call return new ValidationResult;
  1. NullReferenceException was due to ProblemDetailsFactory being null, as ProblemDetailsFactory => HttpContext?.RequestServices?.GetRequiredService<ProblemDetailsFactory>() silently returns null.

Expected behavior

Error message should be actual problem. NullReferenceException is not clear. Perhaps check for null when returning ProblemDetailsFactory property.

Additional context

Add any other context about the problem here.
Include the output of dotnet --info

@pranavkm pranavkm added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Oct 18, 2019
@pranavkm
Copy link
Contributor

Thanks for the issue report @bradlis7. We'd be happy to accept a PR to improve the error message here if you'd like to send one.

@mkArtakMSFT mkArtakMSFT added help wanted Up for grabs. We would accept a PR to help resolve this issue task PRI: 3 - Optional labels Oct 21, 2019
@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone Oct 21, 2019
@Nagarian
Copy link

Hi, i'm issuing the exact same issue only on unit test. On my controller code I'm doing something like this :

public IActionResult Foo([FromBody] Command request)
    {
        ModelState.AddModelError<Command>(a => a.Bar, "Invalid.");
        return ValidationProblem();
    }

On normal execution it works but on unit test I got NullReferenceException.

If I replace ValidationProblem() by BadRequest(ModelState), it works both in unit test and on normal execution. But the response with BadRequest doesn't have all the property of RFC 7807.

How do you handle this error on unit test on your side ?

Thanks in advance

@andrejreimer
Copy link

Hello I am facing the exact same issue as Nagarian, we want to use the RFC Response but our unittests Fail because of that Problem.

Thank you

@Powerz
Copy link

Powerz commented Jun 21, 2020

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Done This issue has been fixed help wanted Up for grabs. We would accept a PR to help resolve this issue task
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

6 participants