EfCore.GenericServices.AspNetCore icon indicating copy to clipboard operation
EfCore.GenericServices.AspNetCore copied to clipboard

Converting IStatusGeneric to ProblemDetails

Open kcaswick opened this issue 4 years ago • 4 comments

Is there any work in progress to support converting IStatusGeneric to ProblemDetails? Or an alternate package that does? It doesn't really make sense to use the ASP.NET 2.1 format for new APIs.

kcaswick avatar Aug 24 '21 01:08 kcaswick

Hi @kcaswick,

There are two issues here:

  1. Is it worth adding support for the ProblemDetails class?
  2. The EfCore.GenericServices.AspNetCore uses Microsoft.AspNetCore.Mvc.Core Version="2.1.1"

Taking the "is it worth adding support for the ProblemDetails class" issue I would say no, as:

  • The ProblemDetails class is aimed at turning exceptions into a better return in an Web API
  • Its pretty easy to manually convert a IStatusGeneric to a ProblemDetail - see the ProblemDetail properties

The "EfCore.GenericServices.AspNetCore uses a out of date version of Microsoft.AspNetCore.Mvc.Core" issue is a good point and at some time I will update it, BUT due to the NuGet definition the latest version of AspNetCore.Mvc.Core used by your app replaces the 2.1 version. And beacause this library uses only the very basic parts of AspNetCore, like ActionResult<T> there isn't a problem.

I'm quite busy at the moment so I won't updating this library to the latest AspNetCore version - maybe when I update any of my libraries to .NET 6. I will leave this issue open to remind me.

JonPSmith avatar Aug 24 '21 10:08 JonPSmith

Landed here wanting to return ProblemDetails myself. Would be a nice to have but as @JonPSmith mentioned its not too difficult to add this in yourself. I achieved it with a BaseController and the following method:

public class BaseController : ControllerBase
{
    public ActionResult<WebApiMessageOnly> WebApiResponse(IStatusGeneric status)
    {
        if (status.IsValid)
        {
            return status.Response();
        }

        status.CopyErrorsToModelState(ModelState);

        return ValidationProblem(ModelState);
    }
}

ConnorJBishop avatar Dec 14 '21 08:12 ConnorJBishop

Thanks @ConnorJBishop for that. I plan to update the libraries to 3.1 and I might add a status.ConvertToProblemDetails() extension method at the same time.

JonPSmith avatar Dec 14 '21 14:12 JonPSmith

I believe my bigger concern had been a 3rd issue, that WebApiMessageAndResult was consistent with the old error response, but not ProblemDetails, so the consistency in format between success and failure responses was lost. If I remember right, Hellang.Middleware.ProblemDetails took care of converting a BadRequestObjectResult to ProblemDetails.

However since then OData became a requirement for my project, and it has its own special error format to return anyway. I'd be fine with closing this issue, unless you want to leave it open for others.

kcaswick avatar Dec 14 '21 15:12 kcaswick