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

ProblemDetails middleware ignore errors from ActionFilterAttribute #2

Closed
lurumad opened this issue Jul 19, 2018 · 3 comments
Closed

Comments

@lurumad
Copy link

lurumad commented Jul 19, 2018

Hi,

I usually try to manage model validation in an ActionFilterAttribute:

public class ValidateModelStateFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (context.ModelState.IsValid)
        {
            return;
        }

        var validation = new ValidationProblemDetails(context.ModelState);

        context.Result = new BadRequestObjectResult(validation);
    }
}

But ProblemDetails middleware ignore errors from this ActionFilterAttribute:

error

I understand the first two conditionals in the method, but not the logic that decides the last:

if (context.Response.ContentLength.HasValue)
{
    return false;
}

if (string.IsNullOrEmpty(context.Response.ContentType))
{
    return true;
}

Regards!

@khellang
Copy link
Owner

khellang commented Jul 19, 2018

Hi!

The assumption is that if Content-Length and Content-Type has been set, the response body has already been written by somone else. They're kinda redundant since we also check HasStarted, but it follows the same rules as the exception handler middleware, so I just kept it for consistency.

Someone else, in this case, is MVC's object result executor. It has already set the content type and written to the body by the time the problem details middleware is reached on the way out. This means it's too late for the middleware to write anything.

In this case it shouldn't matter whether the middleware or MVC's filter writes the response, since it's a ValidationProblemDetails instance. The JSON representation should be the same.

If you really need the MW to handle the response, you can throw a ProblemDetailsException and it should be handled automatically.

@lurumad
Copy link
Author

lurumad commented Jul 20, 2018

Thanks @khellang

Was my fault, I've configured ApiBehaviorOptions and I don't need the ActionFilter and works together:

public static IServiceCollection AddProblemDetails(this IServiceCollection services) =>
    services.Configure<ApiBehaviorOptions>(options =>
    {
        options.InvalidModelStateResponseFactory = context =>
        {
            var problemDetails = new ValidationProblemDetails(context.ModelState)
            {
                Instance = context.HttpContext.Request.Path,
                Status = StatusCodes.Status400BadRequest,
                Type = $"https://httpstatuses.com/400",
                Detail = "Please refer to the errors property for additional details."
            };
            return new BadRequestObjectResult(problemDetails)
            {
                ContentTypes = { "application/problem+json", "application/problem+xml" }
            };
        };
    });

I have to say Hellang.Middleware.ProblemDetails is awesome, great work!!!

Regards!

@lurumad lurumad closed this as completed Jul 20, 2018
@khellang
Copy link
Owner

Nice. Glad you got it working! Incidentally, I just sent a PR to MVC to copy over the result status code to the problem details object, so from 2.2, you don't have to set it explicitly; aspnet/Mvc#8118

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