Skip to content

Commit

Permalink
Fix incorrect cast to IValidator[] (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
WORMrus authored Aug 20, 2024
1 parent 770b7ad commit 0208b5f
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/ValidationDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
global using HotChocolate.Resolvers;
global using FluentValidation;
global using FluentValidation.Results;
using System.Linq;
using System.Runtime.CompilerServices;
using FluentValidation.Internal;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -216,7 +217,14 @@ public static GetValidationContext<TInput> ValidationContextWithStrategy<TInput>
IValidationContext validationContext,
Type validatorType)
{
var validators = (IValidator[])inputValidatorContext.MiddlewareContext.Services.GetServices(validatorType);
// The default DI container returns IValidator[] as the actual underlying type
// However, this is not the case for some custom DI containers, so we add a fallback to cast the array manually
var validatorsEnumerable = inputValidatorContext.MiddlewareContext.Services.GetServices(validatorType);

var validators = validatorsEnumerable as IValidator[]
?? validatorsEnumerable
.OfType<IValidator>()
.ToArray();

if (validators is { Length: 0 })
{
Expand Down

0 comments on commit 0208b5f

Please sign in to comment.