khellang / Middleware

Various ASP.NET Core middleware
MIT License
811 stars 111 forks source link

Custom header gets cleared #146

Open Kumodoushin opened 3 years ago

Kumodoushin commented 3 years ago

In .net 5 webApi project with FluentValidation: When automatic model validation fails, custom header that was sent in request is cleared.

Would you kindly point out what am I missing?

configuration:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpContextAccessor();
    services.AddProblemDetails(options =>
        {
            options.Map<FluentValidation.ValidationException>((ctx, ex) =>
                {
                    var factory = ctx.RequestServices.GetRequiredService<ProblemDetailsFactory>();
            var errors = ex.Errors
                        .GroupBy(x => x.PropertyName)
                        .ToDictionary(x => x.Key, x => x.Select(x => x.ErrorMessage)
                        .ToArray());
                    return factory.CreateValidationProblemDetails(ctx, errors);
                });

                //on requests that pass fluent validation this header is retained
                options.AllowedHeaderNames.Add("x-correlation-id");

                options.ValidationProblemStatusCode = StatusCodes.Status422UnprocessableEntity;
                //error mappings to status codes are removed for brevity
        });
    services.AddControllers()
                .AddNewtonsoftJson()
                .AddFluentValidation(options => options.RegisterValidatorsFromAssemblyContaining<Startup>())
                .AddProblemDetailsConventions();
    //...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseProblemDetails();
    //...
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}
khellang commented 3 years ago

Hi @Kumodoushin! 👋🏻

Can you paste an example request and response? I just want to make sure the response is produced by the problem details middleware. As you've noticed, it's the AllowedHeaderNames setting you should use to keep headers around when producing error responses.

khellang commented 3 years ago

Any updates on this? 😄

Kumodoushin commented 3 years ago

Sorry - too much stuff to do recently. Not sure if this is enough to satisfy your request - since these are postman screenshots and I'm new to this obraz obraz Valid request for comparison: obraz

khellang commented 3 years ago

Thanks. Can I ask where the x-correlation-id is added to the response? What is the order of your middleware?

Kumodoushin commented 3 years ago

The header is added after ProblemDetails - as part of request logging middleware (Serilog with some enricher).

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseExceptionMiddleware(); //ProblemDetails is wrapped here
    app.UseHttpsRedirection();
    app.UseDefaultFiles();
    app.UseStaticFiles();
    app.UseRequestLogging(); //And logging (and adding the header) happens most likely here
    app.UseSession();
    app.UseRouting();
    app.UseIdentityServer();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}