Skip to content

UseDocumentFilter does not put the same properties as the OperationFilter #230

Description

@jgarciadelanoceda

In the issue #223 I raised an issue that was that some schemas were missing references when repeated. There was some work of doing DocumentFilter and avoid this problem in the future.
However there are some gaps between the OperationFilter and the DocumentFilter. Example:

#!/usr/bin/env dotnet

#:sdk Microsoft.NET.Sdk.Web
#:package FluentValidation.AspNetCore@*
#:package MicroElements.Swashbuckle.FluentValidation@*
#:package Swashbuckle.AspNetCore@*
#:package Swashbuckle.AspNetCore.SwaggerGen@*
#:property PublishAot=false
#:property ManagePackageVersionsCentrally=false

using System.Reflection;
using FluentValidation;
using FluentValidation.AspNetCore;
using MicroElements.Swashbuckle.FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Mvc;



var builder = WebApplication.CreateBuilder(args);


builder.Services.AddEndpointsApiExplorer().AddControllers()
.ConfigureApiBehaviorOptions(options =>
{
    options.SuppressInferBindingSourcesForParameters = true;
})
.Services
.AddSwaggerGen()
.AddFluentValidationAutoValidation()
.AddFluentValidationClientsideAdapters()
.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly())
.AddFluentValidationRulesToSwagger(configureRegistration: c => c.UseDocumentFilter = true);

var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.MapControllers();
app.Run();

[ApiController]
public sealed class ApiController : ControllerBase
{
    [HttpGet("/api/hello")]
    public string Hello(HelloRequest request) => $"Hello {request.Name ?? "world"}!";
	
	[HttpGet("/api/hello2")]
    public string HelloTwo(HelloRequest request) => $"Hello {request.Name ?? "world"}!";
}

public sealed class HelloRequest
{
    [FromQuery]
    public string? Name { get; set; }
    [FromHeader(Name = "X-Correlation-Id")]
    public string? XCorrelationId { get; set; }
}

public sealed class HelloRequestValidator : AbstractValidator<HelloRequest>
{
    public HelloRequestValidator()
    {
        RuleFor(x => x.Name)
        .NotEmpty()
        .MaximumLength(10);

        RuleFor(x => x.XCorrelationId)
        .NotEmpty()
        .MaximumLength(36);
    }
}

See the property XCorrelationId that when is DocumentFlter then the FluentValidationRule isn´t reflected on the OpenApi Schema. When OperationFilter it is being shown

As I think that the DocumentFilter is the solution I file this issue ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions