Skip to content

Commit 609e6e4

Browse files
Compatibility with FluentValidation 12.x (#25)
1 parent 347b053 commit 609e6e4

File tree

7 files changed

+27
-11
lines changed

7 files changed

+27
-11
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
build:
1313
strategy:
1414
matrix:
15-
os: ['windows-2019', 'ubuntu-20.04']
15+
os: ['windows-latest', 'ubuntu-latest']
1616
runs-on: ${{ matrix.os }}
1717
steps:
1818
- name: Checkout
@@ -24,6 +24,7 @@ jobs:
2424
uses: actions/setup-dotnet@v2
2525
with:
2626
dotnet-version: |
27+
9.0.x
2728
7.0.100
2829
6.0.x
2930

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "7.0.100",
3+
"version": "9.0.100",
44
"rollForward": "latestFeature"
55
}
66
}

src/FluentValidation.AspNetCore/FluentValidationModelValidatorProvider.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ public virtual IEnumerable<ModelValidationResult> Validate(ModelValidationContex
133133

134134
IValidationContext context = new ValidationContext<object>(mvContext.Model, new PropertyChain(), selector);
135135
context.RootContextData["InvokedByMvc"] = true;
136-
#pragma warning disable CS0618
137-
context.SetServiceProvider(mvContext.ActionContext.HttpContext.RequestServices);
138-
#pragma warning restore CS0618
136+
137+
// For backwards compatibility, store the service provider in the validation context.
138+
// This approach works with both FluentValidation.DependencyInjectionExtensions 11.x
139+
// and FluentValidation.DependencyInjectionExtensions 12.x.
140+
// Do not use context.SetServiceProvider extension method as this no longer
141+
// exists in 12.x.
142+
context.RootContextData["_FV_ServiceProvider"] = mvContext.ActionContext.HttpContext.RequestServices;
139143

140144
if (interceptor != null) {
141145
// Allow the user to provide a customized context

src/FluentValidation.Tests.AspNetCore/DependencyInjectionTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace FluentValidation.Tests;
1212

1313
#pragma warning disable CS0618
1414

15+
#if !NET9_0
1516
public class DependencyInjectionTests : IClassFixture<WebAppFixture> {
1617
private readonly ITestOutputHelper _output;
1718
private readonly HttpClient _client;
@@ -51,3 +52,4 @@ public async Task Resolves_explicit_child_validator_for_collection() {
5152
result.GetError("Children[0].Name").ShouldEqual("NotNullInjected");
5253
}
5354
}
55+
#endif

src/FluentValidation.Tests.AspNetCore/FluentValidation.Tests.AspNetCore.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22
<PropertyGroup>
3-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
3+
<TargetFrameworks>net6.0;net7.0;net9.0</TargetFrameworks>
44
<IsPackable>false</IsPackable>
55
<RootNamespace>FluentValidation.Tests</RootNamespace>
66
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
@@ -25,6 +25,13 @@
2525
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.0" />
2626
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.0" />
2727
</ItemGroup>
28+
<ItemGroup Condition="'$(TargetFramework)'=='net9.0'">
29+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
30+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0" />
31+
<!-- For .net 9 builds also target FV 12 -->
32+
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.0.0" />
33+
<PackageReference Include="FluentValidation" Version="12.0.0" />
34+
</ItemGroup>
2835
<ItemGroup>
2936
<ProjectReference Include="..\FluentValidation.AspNetCore\FluentValidation.AspNetCore.csproj" />
3037
</ItemGroup>

src/FluentValidation.Tests.AspNetCore/TestModels.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ public class ChildModel7 {
422422
public string Name { get; set; }
423423
}
424424

425+
#if !NET9_0
425426
public class InjectsExplicitChildValidator : AbstractValidator<ParentModel> {
426427
public InjectsExplicitChildValidator() {
427428
#pragma warning disable CS0618
@@ -443,6 +444,7 @@ public InjectsExplicitChildValidatorCollection() {
443444
#pragma warning restore CS0618
444445
}
445446
}
447+
#endif
446448

447449
public class BadAsyncModel {
448450
public int Id { get; set; }

src/FluentValidation.Tests.AspNetCore/TypeFilterTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public async Task Finds_and_executes_validator() {
4242
fv.RegisterValidatorsFromAssemblyContaining<TestController>();
4343
});
4444
});
45-
var result = await client.GetErrors("InjectsExplicitChildValidator");
45+
var result = await client.GetErrors("Test1");
4646

4747
// Validator was found and executed so field shouldn't be valid.
48-
result.IsValidField("Child.Name").ShouldBeFalse();
48+
result.IsValidField("Name").ShouldBeFalse();
4949

5050
}
5151

@@ -54,14 +54,14 @@ public async Task Filters_types() {
5454
var client = _webApp.CreateClientWithServices(services => {
5555
services.AddMvc().AddNewtonsoftJson().AddFluentValidation(fv => {
5656
fv.RegisterValidatorsFromAssemblyContaining<TestController>(scanResult => {
57-
return scanResult.ValidatorType != typeof(InjectsExplicitChildValidator);
57+
return scanResult.ValidatorType != typeof(TestModelValidator);
5858
});
5959
});
6060
});
6161

62-
var result = await client.GetErrors("InjectsExplicitChildValidator");
62+
var result = await client.GetErrors("Test1");
6363

6464
// Should be valid as the validator was skipped.
65-
result.IsValidField("Child.Name").ShouldBeTrue();
65+
result.IsValidField("Name").ShouldBeTrue();
6666
}
6767
}

0 commit comments

Comments
 (0)