Skip to content

Commit

Permalink
Update dependency MicroElements.Swashbuckle.FluentValidation to v6 (#…
Browse files Browse the repository at this point in the history
…1691)

* Update dependency MicroElements.Swashbuckle.FluentValidation to v6

* private properties are weird

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: David Driscoll <david.driscoll@gmail.com>
  • Loading branch information
renovate[bot] and david-driscoll authored Dec 31, 2023
1 parent 583f048 commit 41b8835
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
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

0 comments on commit 41b8835

Please sign in to comment.