-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Create and include an analyzer that validates action method return types match the declared ActionResult<T> #8535
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
Comments
I'm currently facing a similar dilemma. This seems like a nice place to chat about it without creating a new issue. Essentially the existing codebase I'm working in has a public abstract class BaseResponse
{
public virtual int HttpStatusCode { get; set; } = 200;
} The nice thing about this is that we can keep our controllers super light, route the message off with MediatR to a handler and all our handlers return an implementation of The existing codebase currently has an extension method on I didn't really like that much, as I'd like my Action methods to have definite return types, so I started refactoring by creating an [ApiController, Produces("application/json")]
public abstract class ApiControllerBase : ControllerBase
{
protected ObjectResult StatusCode(BaseResponse response)
=> StatusCode(response.HttpStatusCode, response);
} but then when I started specifying all the Action methods return types explicitly, I realised that returning Trying to think of other options, I've considered:
Problem with the last two solutions is that this requires me to add a dependency to the Mvc package but in our solution that's not going to work as we have "Client" projects (essentially a client SDK for our service) with the Response models and then "Service" projects which ref the Client for the models and are our MVC projects. I wouldn't want our Client projects to have to reference all of Mvc, and indeed it'd actually be impossible as some of our consumers are net47 projects. So for those last two options to be viable I'd need to have All in all, I guess my best options right now are:
// Example of such an action filter (not tested yet)
public class SetResponseStatusCodeFromBaseResponseActionFilter : ResultFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext context)
{
if (context.Result is ObjectResult objectResult &&
objectResult.Value is BaseResponse baseResponse)
{
objectResult.StatusCode = baseResponse.HttpStatusCode;
}
base.OnResultExecuted(context);
}
} Would love to hear any other suggestions! |
I guess the other approach might be to have my MediatR handlers all return Would still have the same problem you've highlight @DamianEdwards that there's desperate need for Analyzer validation there, but that'll be solved eventually. |
I think this is something we all struggle with, "how can we have a business layer that can return a response that easily adjusts the status code?". I think a
I also just discovered that the ProducesResponseType analyzer can only handle const status codes, so neither of my 2 best options look like they won't benefit much from this unless it can be updated to dig deeper into the user's code. As such, I'll go for the ActionFilter solution atm as it reduces the boilerplate and provides type safety. TODO: Investigate an ActionFilter that in development mode could throw or log errors if response type != attributed response types. |
Shouldn't it be feasible to return implicitly return I agree with @benmccallum that ultimately my chief issue is returning
I apologize for second-guessing someone's work I'm on the outside of, looking in to; I'm sure it looks easier from here than it is. |
Or perhaps I misunderstood @pranavkm 's comment and the analyzer is not a substitute for addressing this pattern issue long-term, but rather in addition to it, to help developers find problems in older code. |
Thanks for contacting us. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Add an analyzer that validates the return types from an action method returning
ActionResult<T>
, e.g. don't let it returnObjectResult
with a different T value, e.g. the following is invalid:The text was updated successfully, but these errors were encountered: