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

Small bug, you can not reuse ValidationContext in ValidationBehaviour.cs #1082

Open
gevorgter opened this issue Dec 31, 2023 · 0 comments
Open

Comments

@gevorgter
Copy link

gevorgter commented Dec 31, 2023

File src/Application/Common/Behaviours/ValidationBehaviour.cs

Lines # 19-23

var context = new ValidationContext<TRequest>(request);

var validationResults = await Task.WhenAll(
    _validators.Select(v =>v.ValidateAsync(context, cancellationToken))
);

Unfortunately context is not thread safe and adds validation errors to collection (somewhere inside).

Steps to reproduce, have 2 validators for the same Request object, you will see that messages will duplicate. And occasionally (if you use real DB validation like BeUniqueTitle), you'll get error 'collection was modified..., when 2 threads will try to add error message simultaneously.
I rewrote it using simple foreach

List<FluentValidation.Results.ValidationFailure> failures = [];

foreach (var v in _validators)
{
    var context = new ValidationContext<TRequest>(request);
    var r = await v.ValidateAsync(context, cancellationToken);
    if (!r.IsValid)
        failures.AddRange(r.Errors);
}

Source for ValidationContext, Line 86 has "internal List Failures { get; }" which makes it not thread safe.
https://github.com/FluentValidation/FluentValidation/blob/main/src/FluentValidation/IValidationContext.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant