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

MapStatusCode not respected for MVC validation errors #185

Open
arjun-1 opened this issue Sep 8, 2022 · 1 comment
Open

MapStatusCode not respected for MVC validation errors #185

arjun-1 opened this issue Sep 8, 2022 · 1 comment

Comments

@arjun-1
Copy link

arjun-1 commented Sep 8, 2022

I want to change the ProblemDetails.Type for a 422 response that is the result of MVC validation error (after having called AddProblemDetailsConventions).

Reading the documentation of MapStatusCode, this should be possible with:

services
  .AddProblemDetails(options =>
  {
    options.MapStatusCode = ctx => // change Type in case of 422
  }
  .AddProblemDetailsConventions()

I find however that the Type is not changed at all. In order to do so, I must replace the MvcProblemDetailsFactory (after calling AddProblemDetailsConventions) and substitute my own which has a modified CreateValidationProblemDetails:

services.Replace(
  ServiceDescriptor.Singleton<MvcProblemDetailsFactory, MyProblemDetailsFactory>()
);

Probably this is because Options.MapStatusCode is not being used in CreateValidationProblemDetails in ProblemDetailsFactory? Is there a way to change the produced Type using the functionality of this library?

@khellang
Copy link
Owner

Hi @arjun-1! 👋🏻

Sorry for the late reply. Maybe you could try to set the OnBeforeWriteDetails hook? It should let you modify the ProblemDetails object before it's written to the response:

options.OnBeforeWriteDetails = (_, details) =>
{
details.Extensions[nameof(ProblemDetailsOptions.OnBeforeWriteDetails)] = true;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants