diff --git a/Directory.Packages.props b/Directory.Packages.props index 6d5e98768..69d18fd94 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -74,7 +74,7 @@ <PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" /> <PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.4.0" /> <PackageVersion Include="Microsoft.Reactive.Testing" Version="6.0.0" /> - <PackageVersion Include="MicroElements.Swashbuckle.FluentValidation" Version="5.7.0" /> + <PackageVersion Include="MicroElements.Swashbuckle.FluentValidation" Version="6.0.0" /> <PackageVersion Include="Newtonsoft.Json" Version="13.0.3" /> <PackageVersion Include="NetTopologySuite" Version="2.5.0" /> <PackageVersion Include="NetTopologySuite.Features" Version="2.1.0" /> diff --git a/src/AspNetCore/Conventions/FluentValidationConvention.cs b/src/AspNetCore/Conventions/FluentValidationConvention.cs index be518f5a2..dc56e7b14 100644 --- a/src/AspNetCore/Conventions/FluentValidationConvention.cs +++ b/src/AspNetCore/Conventions/FluentValidationConvention.cs @@ -1,6 +1,7 @@ using System.Reflection; using FluentValidation.AspNetCore; using FluentValidation.Validators; +using MicroElements.OpenApi.FluentValidation; using MicroElements.Swashbuckle.FluentValidation; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; @@ -33,7 +34,13 @@ private static void AddFluentValidationRules(IServiceCollection services) .WithApply( context => { - var propertyType = context.ReflectionContext.PropertyInfo?.DeclaringType ?? context.ReflectionContext.ParameterInfo?.ParameterType; + var ruleContext = ((ValidationRuleContext)context + .GetType() + .GetProperties(BindingFlags.Instance | BindingFlags.NonPublic) + .First(z => z.PropertyType == typeof(ValidationRuleContext)) + .GetValue(context)) + .GetReflectionContext(); + var propertyType = ruleContext?.PropertyInfo?.DeclaringType; if (propertyType == typeof(string)) { context.Schema.Properties[context.PropertyKey].MinLength = 1; @@ -47,7 +54,13 @@ private static void AddFluentValidationRules(IServiceCollection services) .WithApply( context => { - var propertyType = context.ReflectionContext.PropertyInfo?.DeclaringType ?? context.ReflectionContext.ParameterInfo?.ParameterType; + var ruleContext = ((ValidationRuleContext)context + .GetType() + .GetProperties(BindingFlags.Instance | BindingFlags.NonPublic) + .First(z => z.PropertyType == typeof(ValidationRuleContext)) + .GetValue(context)) + .GetReflectionContext(); + var propertyType = ruleContext?.PropertyInfo?.DeclaringType; if (propertyType != null && ( ( propertyType.IsValueType && Nullable.GetUnderlyingType(propertyType) == null ) || propertyType.IsEnum )) @@ -64,11 +77,15 @@ private static void AddFluentValidationRules(IServiceCollection services) .WithApply( context => { + var ruleContext = ((ValidationRuleContext)context + .GetType() + .GetProperties(BindingFlags.Instance | BindingFlags.NonPublic) + .First(z => z.PropertyType == typeof(ValidationRuleContext)) + .GetValue(context)) + .GetReflectionContext(); context.Schema.Properties[context.PropertyKey].Nullable = context.PropertyValidator is not (INotNullValidator or INotEmptyValidator) - || ( context.ReflectionContext.ParameterInfo is { } pai && getNullableValue(pai.GetNullability(), pai.ParameterType) ) - || ( context.ReflectionContext.PropertyInfo is PropertyInfo pi && getNullableValue(pi.GetNullability(), pi.PropertyType) ) - || ( context.ReflectionContext.PropertyInfo is FieldInfo fi && getNullableValue(fi.GetNullability(), fi.FieldType) ) + || ( ruleContext.PropertyInfo is FieldInfo fi && getNullableValue(fi.GetNullability(), fi.FieldType) ) ; static bool getNullableValue(Nullability nullability, Type propertyType)