micro-elements / MicroElements.Swashbuckle.FluentValidation

Use FluentValidation rules instead of ComponentModel attributes
MIT License
379 stars 60 forks source link

.net7.0: GetValidator for type 'System.ReadOnlySpan`1[System.Byte]' fails. #124

Open ChrisRichner opened 1 year ago

ChrisRichner commented 1 year ago

Hi there

I'm facing the following System.ArgumentException when updating my service from .net 6.0 to .net 7.0. This exception is thrown when the /swagger site is first accessed.

warn: MicroElements.Swashbuckle.FluentValidation.FluentValidationRules[0]
      GetValidator for type 'System.ReadOnlySpan`1[System.Byte]' fails.
      System.ArgumentException: GenericArguments[0], 'System.ReadOnlySpan`1[System.Byte]', on 'FluentValidation.IValidator`1[T]' violates the constraint of type 'T'.
       ---> System.TypeLoadException: GenericArguments[0], 'System.ReadOnlySpan`1[System.Byte]', on 'FluentValidation.IValidator`1[T]' violates the constraint of type parameter 'T'.
         at System.RuntimeTypeHandle.Instantiate(RuntimeType inst)
         at System.RuntimeType.MakeGenericType(Type[] instantiation)
         --- End of inner exception stack trace ---
         at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
         at System.RuntimeType.MakeGenericType(Type[] instantiation)
         at FluentValidation.ValidatorFactoryBase.GetValidator(Type type) in /_/src/FluentValidation/ValidatorFactoryBase.cs:line 42
         at MicroElements.Swashbuckle.FluentValidation.FluentValidationRules.Apply(OpenApiSchema schema, SchemaFilterContext context) in /home/runner/work/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation/src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/FluentValidationRules.cs:line 92

Please let me know if you need further Infos, thanks

petriashev commented 1 year ago

Can you check latest beta version and updated Readme for minimal API?

petriashev commented 1 year ago

@ChrisRichner is this issue still actual?

ChristophRichner commented 1 year ago

@petriashev Hi Alexey

I've updated to beta3 and followed the minimal API Docs as close as I could. We do not use minimal API.

The issue still remains for us.

Thank you for your time 👍

NikAtIdeally commented 11 months ago

Dotnet 8 with Swashbuckle.FluentValidation v 6.0.0 issue still remains and we are seeing this warning:

warn: MicroElements.Swashbuckle.FluentValidation.FluentValidationRules[0] GetValidators for type 'System.ReadOnlySpan'1[System.Byte]' failed System.ArgumentException: GenericArguments[0], 'System.ReadOnlySpan'1[System.Byte]', on 'FluentValidation.IValidator'1[T]' violates the constraint of type 'T'. ---> System.TypeLoadException: GenericArguments[0], 'System.ReadOnlySpan'1[System.Byte]', on 'FluentValidation.IValidator'1[T]' violates the constraint of type parameter 'T'. at System.RuntimeTypeHandle.Instantiate(QCallTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type) at System.RuntimeTypeHandle.Instantiate(RuntimeType inst) at System.RuntimeType.MakeGenericType(Type[] instantiation) --- End of inner exception stack trace --- at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e) at System.RuntimeType.MakeGenericType(Type[] instantiation) at MicroElements.OpenApi.FluentValidation.ValidatorRegistryExtensions.GetValidators(IServiceProvider serviceProvider, Type modelType, ISchemaGenerationOptions options)+MoveNext() in /home/runner/work/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation/src/MicroElements.OpenApi.FluentValidation/FluentValidation/ValidatorRegistry/ValidatorRegistryExtensions.cs:line 73 at System.Collections.Generic.LargeArrayBuilder'1.AddRange(IEnumerable'1 items) at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable'1 source) at MicroElements.Swashbuckle.FluentValidation.FluentValidationRules.<>c__DisplayClass5_0.<Apply>b__0() in /home/runner/work/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation/src/MicroElements.Swashbuckle.FluentValidation/Swashbuckle/FluentValidationRules.cs:line 77 at MicroElements.OpenApi.Core.Functional.Try[T](Func'1 func) in /home/runner/work/MicroElements.Swashbuckle.FluentValidation/MicroElements.Swashbuckle.FluentValidation/src/MicroElements.OpenApi.FluentValidation/Core/Functional.cs:line 19 Is there any way to make this warning go away? Even if I remove all fluent validation models from the project I am still seeing this.

petriashev commented 11 months ago

Hi everybvody! Can you provide any repro for this? I tried to reproduce but could not

xxpau-eg commented 1 month ago

I think this happens when you have a ReadOnlySpan in your swagger schema, e.g. as parameter to a method or as a return value.