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

Custom header gets cleared #146

Open
Kumodoushin opened this issue Sep 22, 2021 · 5 comments
Open

Custom header gets cleared #146

Kumodoushin opened this issue Sep 22, 2021 · 5 comments

Comments

@Kumodoushin
Copy link

Kumodoushin commented Sep 22, 2021

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
Copy link
Owner

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
Copy link
Owner

Any updates on this? 😄

@Kumodoushin
Copy link
Author

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
Copy link
Owner

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

@Kumodoushin
Copy link
Author

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

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