Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependency MicroElements.Swashbuckle.FluentValidation to v6 #1691

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
27 changes: 22 additions & 5 deletions src/AspNetCore/Conventions/FluentValidationConvention.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 ))
Expand All @@ -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)
Expand Down
Loading