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

Converting IStatusGeneric to ProblemDetails #5

Open
kcaswick opened this issue Aug 24, 2021 · 4 comments
Open

Converting IStatusGeneric to ProblemDetails #5

kcaswick opened this issue Aug 24, 2021 · 4 comments

Comments

@kcaswick
Copy link

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.

@JonPSmith
Copy link
Owner

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.

@ConnorJBishop
Copy link

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);
    }
}

@JonPSmith
Copy link
Owner

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.

@kcaswick
Copy link
Author

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.

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

3 participants