zymlabs / nswag-fluentvalidation

Use FluentValidation rules instead of ComponentModel attributes to define swagger schema.
MIT License
60 stars 13 forks source link

Support for Fluent 10.0.0 #8

Closed diogonborges closed 3 years ago

diogonborges commented 3 years ago

This library and its setup on readme does not currently startup the API.

The root cause of this is that Fluent now registers the IValidator as a Scoped service instead of a Transient making a Singleton Singleton FluentValidationSchemaProcessor impossible to be registered.

ZymLabs.NSwag.FluentValidation.FluentValidationSchemaProcessor': Cannot consume scoped service 'FluentValidation.IValidatorFactory' from singleton 'ZymLabs.NSwag.FluentValidation.FluentValidationSchemaProcessor'.

diogonborges commented 3 years ago

My suggestion would be:


services.AddOpenApiDocument((settings, serviceProvider) =>
{
    var fluentValidationSchemaProcessor = serviceProvider.CreateScope().ServiceProvider.GetService<FluentValidationSchemaProcessor>();

    // Add the fluent validations schema processor
    settings.SchemaProcessors.Add(fluentValidationSchemaProcessor);
});

// Add the FluentValidationSchemaProcessor as a singleton
services.AddScoped<FluentValidationSchemaProcessor>();
geoffreytran commented 3 years ago

Yes, I'll get the readme updated as soon as I can test it. It should work fine as a scoped service.

geoffreytran commented 3 years ago

It looks like there are breaking changes to FluentValidation 10 that marks some of the classes such as PropertyRule as internal. It will take a bit more time to find an alternative method to make this library compatible.

geoffreytran commented 3 years ago

I've updated the library to support FluentValidation 10, it's released as 0.5.0-alpha0005 on NuGet. Give it a try and let me know if there are any issues. I'll publish the 0.5.0 release if everything checks out. It appears to be working on my end, but there could be cases that were not handled completely as the changes required from FluentValidation 9 to 10 internally was pretty drastic.

vai commented 3 years ago

Thank you, we will give this a test. What sort of changes should we be aware of when consuming this library after the change to use FluentValidation v10?

diogonborges commented 3 years ago

@geoffreytran @vai we were using happy path straight from the Readme. The the basic usage needed some changes? I.e. the example from the Readme is updated on the alpha? Sorry I still didn't check it!

pedrosalider commented 3 years ago

I just integrated the alpha version and everything seems to be working properly. We got the same results as when we were using the previous versions of FluentValidation, but @geoffreytran can you please let us know what sort of changes should we be aware of?

vai commented 3 years ago

@geoffreytran This seems good - Diogo's off on leave - I can tell you we've tested the change and it's working, and now just need a release version.

geoffreytran commented 3 years ago

In terms of changes, there were not any new functionality changes, but due to the removal of access to the following classes in v10 Upgrading to V10 I had to adjust how we access the validators. Readme has been updated with the minor change to use scoped services.

RuleBuilder PropertyRule CollectionPropertyRule IncludeRule

Changeset for V10 support https://github.com/zymlabs/nswag-fluentvalidation/commit/c236a4eeb9e7296dac91c5a071e45c3683c2765f

The majority of the changes are surrounding accessing the validation property rules and include rules. Unit tests passed, but I would be on the lookout for any edge case issues on matching of the property rules if you have custom property validators or uncommon validations. I'll publish the release if there aren't any issues found.

vai commented 3 years ago

Our test suites were successful too. I'll advise through new issues if there's anything further from the v10 upgrade, so please feel free to close this one. Thanks for the support.

geoffreytran commented 3 years ago

Thank you for the feedback. The release has been pushed.

diogonborges commented 3 years ago

Great stuff everybody. Next time,with time, I'll contribute with actual code,instead of only pointing out an issue! ;)

diogonborges commented 3 years ago

Great stuff everybody! Sorry I've been away and couldn't catch up sooner on this topic!