Skip to content

When one or more endpoints have the same Dto the firts one gets the FluentValidationRule and the next ones are not getting it #223

Description

@jgarciadelanoceda

This is a simple use case:

#!/usr/bin/env dotnet

#:sdk Microsoft.NET.Sdk.Web
#:package FluentValidation.AspNetCore@*
#:package MicroElements.Swashbuckle.FluentValidation@*
#:package Swashbuckle.AspNetCore@*
#: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(o => o.UseAllOfForMultipleRules = false);

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

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

I like to put the RemoveUnusedQuerySchemas = true but finally I put it as false because I am losing the FluentValidationRules on the migration from version 6.0.0.0 to 7.1.8

I am happy to contribute anyway

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions