RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.67k stars 1.23k forks source link

ASP.NET Core - Swagger Generator - Support FluentValidation Rules #1722

Open ideoclickVanessa opened 5 years ago

ideoclickVanessa commented 5 years ago

I've chosen NSwag over Swashbuckle for my api because I really like how well the NSwag Studio is able to hit the swagger endpoint and simply create a C# client or a TypeScript client for my api. I've also chosen FluentValidation over DataAnnotations as my incoming request model validation since it's still quite easy to use even in complex validation scenarios. However, the downside of this choice is that the NSwag Swagger Generator does not seem to support reading / outputting the FluentValidation rules in the generated Swagger document.

Is there any chance that NSwag could be updated to support the FluentValidation rules? Perhaps in an addon package similar to the one someone made for Swashbuckle if you are worried about bloat in the main generator package.

RicoSuter commented 5 years ago

I think it shouldnt be that hard to rewrite Swashbuckle filter as an NSwag operation processor...

davidhenley commented 5 years ago

That would be awesome.

polszewski commented 5 years ago

Any chance seeing this soon?

deaquino commented 5 years ago

I'd love it!

DanShoubridge commented 5 years ago

+1

nojaf commented 5 years ago

I'm trying to have something similar. If you look at the fluent validator docs you need a [Validator(typeof(PersonValidator))] attribute and a piece in your Controller

if(! ModelState.IsValid) { // re-render the view when validation failed.
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }

I'm able to customize the Controller part in the liquid templates but I can't seem to find where I can customize the template to add the attribute.

@RSuter could that be done as well? I think I need an addition to Class.Annotations.liquid from NJsonSchema but setting that file in my custom templates folder doesn't seem to work.

RicoSuter commented 5 years ago

@nojaf I think this should work - and you should be able to override the Class.Annotations.liquid file of NJS... maybe the template model does not expose enough information for you to generate the validator correctly - if this is the case we need to add more properties to the model in NJS

FYI: This here is about reading the validator rules to generate a correct swagger spec - but you are talking about generating validator rules in the controller code generator... maybe you should create a new issue for this.

NickRimmer commented 5 years ago

Any news? (= Someone did it already or maybe find some solution for NSwag?

geoffreytran commented 4 years ago

I ended up porting the MicroElements.Swashbuckle.FluentValidation package to NSwag in case anyone is still looking for a solution.

Github: https://github.com/zymlabs/nswag-fluentvalidation NuGet: https://www.nuget.org/packages/ZymLabs.NSwag.FluentValidation/

daniel-white commented 4 years ago

I'd love to see @geoffreytran's library merged into this project to get first class support! thanks for writing it!

bbarry commented 4 years ago

@geoffreytran heads up on some changes you'll need to consider if/when you update to FluentValidation 9, see the issue I linked yesterday to FluentValidation#1434. Also for anyone else who might have ported this themselves or have a similar library or want to work on this issue.

geoffreytran commented 4 years ago

@bbarry Thanks for the heads up! I'll have a look as soon as I can and get those updates in to match.