Skip to content

Commit

Permalink
Fix a bug where async conditions were sometimes run synchronously.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed Apr 17, 2021
1 parent a7f019d commit de7a28b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
10.0.4 - 17 April 2021
Fix a bug where async conditions were sometimes run synchronously.

10.0.3 - 15 April 2021
Fix ArgumentOutOfRangeException when condition returns false for RuleForEach containing multiple components (#1698)

Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>10.0.3</VersionPrefix>
<VersionPrefix>10.0.4</VersionPrefix>
<VersionSuffix></VersionSuffix>
<!-- Use CI build number as version suffix (if defined) -->
<!--<VersionSuffix Condition="'$(GITHUB_RUN_NUMBER)'!=''">ci-$(GITHUB_RUN_NUMBER)</VersionSuffix>-->
Expand Down
23 changes: 16 additions & 7 deletions src/FluentValidation/Internal/PropertyRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ TProperty PropertyFunc(T instance)
foreach (var step in Components) {
context.MessageFormatter.Reset();

if (!step.InvokeCondition(context)) {
continue;
}

if (step.HasAsyncCondition && !step.InvokeAsyncCondition(context, CancellationToken.None).GetAwaiter().GetResult()) {
continue;
}

if (step.ShouldValidateAsynchronously(context)) {
InvokePropertyValidatorAsync(context, accessor, propertyName, step, default).GetAwaiter().GetResult();
}
Expand Down Expand Up @@ -190,6 +198,14 @@ TProperty PropertyFunc(T instance)
cancellation.ThrowIfCancellationRequested();
context.MessageFormatter.Reset();

if (!validator.InvokeCondition(context)) {
continue;
}

if (!await validator.InvokeAsyncCondition(context, cancellation)) {
continue;
};

if (validator.ShouldValidateAsynchronously(context)) {
await InvokePropertyValidatorAsync(context, accessor, propertyName, validator, cancellation);
}
Expand Down Expand Up @@ -219,8 +235,6 @@ TProperty PropertyFunc(T instance)
}

private async Task InvokePropertyValidatorAsync(ValidationContext<T> context, Lazy<TProperty> accessor, string propertyName, RuleComponent<T,TProperty> component, CancellationToken cancellation) {
if (!component.InvokeCondition(context)) return;
if (!await component.InvokeAsyncCondition(context, cancellation)) return;
bool valid = await component.ValidateAsync(context, accessor.Value, cancellation);

if (!valid) {
Expand All @@ -232,11 +246,6 @@ TProperty PropertyFunc(T instance)
}

private protected void InvokePropertyValidator(ValidationContext<T> context, Lazy<TProperty> accessor, string propertyName, RuleComponent<T, TProperty> component) {
if (!component.InvokeCondition(context)) return;
if (component.HasAsyncCondition && !component.InvokeAsyncCondition(context, CancellationToken.None).GetAwaiter().GetResult()) {
return;
}

bool valid = component.Validate(context, accessor.Value);

if (!valid) {
Expand Down

0 comments on commit de7a28b

Please sign in to comment.