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

Is it possible to control the schema response in ASP.NET Core? #5

Closed
xplatsolutions opened this issue Sep 5, 2023 · 8 comments · Fixed by #6
Closed

Is it possible to control the schema response in ASP.NET Core? #5

xplatsolutions opened this issue Sep 5, 2023 · 8 comments · Fixed by #6
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@xplatsolutions
Copy link

I would like to set my own response schema in the 400 Bad Request response.

e.g.

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-0a4404bdf847cb8cf5f696aac649ee61-5dff3d630c7d9c63-00",
    "errors": {
        "ShownGenderOnProfile": [
            "ShownGenderOnProfile must not be empty"
        ]
    }
}

I am using the auto-validation package SharpGrip.FluentValidation.AutoValidation.Mvc.

@mvdgun
Copy link
Member

mvdgun commented Sep 5, 2023

Hi @xplatsolutions, interesting feature. Perhaps it's already possible by customizing the default problem details as explained here: https://learn.microsoft.com/en-us/aspnet/core/web-api/handle-errors?view=aspnetcore-7.0#implement-problemdetailsfactory.

Let me look into it and if it's not possible I'll integrate it into the MVC package and perhaps the Minimal APIs package.

@xplatsolutions
Copy link
Author

xplatsolutions commented Sep 6, 2023

That would be amazing if we figure it out, can be tricky but I imagine something like adding our own JSON for each error.

Example:

{
   "errorCode":200,
   "domainCode":100,
   "errors":{
      "ShownGenderOnProfile":[
         {
            "domainCode":4,
            "errorCode":20,
            "errorMessage":"ShownGenderOnProfile must not be empty",
            "data":null
         }
      ]
   }
}

@mvdgun
Copy link
Member

mvdgun commented Sep 6, 2023

I just finished the initial work for a custom result factory. In your case it would work something like this:

using SharpGrip.FluentValidation.AutoValidation.Mvc.Extensions;

builder.Services.AddFluentValidationAutoValidation(configuration =>
{
    // Replace the default result factory with a custom implementation.
    configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});

public class CustomResultFactory : IFluentValidationAutoValidationResultFactory
{
    public IActionResult CreateActionResult(ActionExecutingContext context, ValidationProblemDetails? validationProblemDetails)
    {
        return new BadRequestObjectResult(new {Title = "Validation errors", ValidationErrors = validationProblemDetails?.Errors});
    }
}

You can return a fully custom response when the model state is invalid. You will be able to access the ValidationProblemDetails containing the validation errors and the full ActionExecutingContext. Would something like this help you?

@xplatsolutions
Copy link
Author

xplatsolutions commented Sep 6, 2023

This looks like exactly what I need to create my own custom response for all the errors! I can try it out ASAP if you want :)

@mvdgun mvdgun linked a pull request Sep 6, 2023 that will close this issue
@mvdgun mvdgun closed this as completed in #6 Sep 6, 2023
@mvdgun
Copy link
Member

mvdgun commented Sep 6, 2023

Hi @xplatsolutions, I just released the v1.1.0 versions of the packages containing the option for providing a custom result factory.

@mvdgun mvdgun added the enhancement New feature or request label Sep 6, 2023
@mvdgun mvdgun self-assigned this Sep 6, 2023
@xplatsolutions
Copy link
Author

I will check it out ASAP! Thanks for this one

@xplatsolutions
Copy link
Author

Confirming the approach worked like a charm! Thank you

@mvdgun
Copy link
Member

mvdgun commented Sep 6, 2023

Awesome! Thanks for getting back.

@mvdgun mvdgun added this to the v1.1 milestone Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants