Skip to content

Commit 48d9b82

Browse files
authored
Add set up for validations generator (#60485)
1 parent e215183 commit 48d9b82

File tree

42 files changed

+688
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+688
-90
lines changed

Diff for: AspNetCore.sln

+41-75
Large diffs are not rendered by default.

Diff for: eng/testing/linker/SupportFiles/Directory.Build.targets

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<ProjectReference Include="$(RepoRoot)src\SignalR\clients\csharp\Client\src\Microsoft.AspNetCore.SignalR.Client.csproj" />
7777
<ProjectReference Include="$(RepoRoot)src\SignalR\server\SignalR\src\Microsoft.AspNetCore.SignalR.csproj" />
7878
<ProjectReference Include="$(RepoRoot)src\OpenApi\src\Microsoft.AspNetCore.OpenApi.csproj" />
79-
<ProjectReference Include="$(RepoRoot)src\Http\Http.Extensions\gen\Microsoft.AspNetCore.Http.RequestDelegateGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
79+
<ProjectReference Include="$(RepoRoot)src\Http\Http.Extensions\gen\Microsoft.AspNetCore.Http.RequestDelegateGenerator\Microsoft.AspNetCore.Http.RequestDelegateGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
8080
</ItemGroup>
8181

8282
</Project>

Diff for: src/Framework/App.Ref/src/Microsoft.AspNetCore.App.Ref.sfxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
OutputItemType="AspNetCoreAnalyzer"
7373
ReferenceOutputAssembly="false" />
7474

75-
<ProjectReference Include="$(RepoRoot)src\Http\Http.Extensions\gen\Microsoft.AspNetCore.Http.RequestDelegateGenerator.csproj"
75+
<ProjectReference Include="$(RepoRoot)src\Http\Http.Extensions\gen\Microsoft.AspNetCore.Http.RequestDelegateGenerator\Microsoft.AspNetCore.Http.RequestDelegateGenerator.csproj"
7676
Private="false"
7777
OutputItemType="AspNetCoreAnalyzer"
7878
ReferenceOutputAssembly="false" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
6+
<IsPackable>false</IsPackable>
7+
<IsAnalyzersProject>true</IsAnalyzersProject>
8+
<AddPublicApiAnalyzers>false</AddPublicApiAnalyzers>
9+
<Nullable>enable</Nullable>
10+
<WarnOnNullable>true</WarnOnNullable>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="All" IsImplicitlyDefined="true" Version="$(MicrosoftCodeAnalysisVersion_LatestVS)" />
15+
<PackageReference Include="Microsoft.CodeAnalysis.Common" PrivateAssets="All" IsImplicitlyDefined="true" Version="$(MicrosoftCodeAnalysisVersion_LatestVS)" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Extensions.Tests" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<Compile Include="$(SharedSourceRoot)IsExternalInit.cs" LinkBase="Shared" />
24+
<Compile Include="$(SharedSourceRoot)HashCode.cs" LinkBase="Shared" />
25+
<Compile Include="$(SharedSourceRoot)RoslynUtils\BoundedCacheWithFactory.cs" LinkBase="Shared" />
26+
<Compile Include="$(SharedSourceRoot)RoslynUtils\WellKnownTypeData.cs" LinkBase="Shared" />
27+
<Compile Include="$(SharedSourceRoot)RoslynUtils\WellKnownTypes.cs" LinkBase="Shared" />
28+
<Compile Include="$(SharedSourceRoot)RoslynUtils\CodeWriter.cs" LinkBase="Shared" />
29+
<Compile Include="$(RepoRoot)\src\Http\Http.Extensions\gen\Microsoft.AspNetCore.Http.RequestDelegateGenerator\StaticRouteHandlerModel\InvocationOperationExtensions.cs" LinkBase="Shared" />
30+
<Compile Include="$(SharedSourceRoot)Diagnostics\AnalyzerDebug.cs" LinkBase="Shared" />
31+
</ItemGroup>
32+
33+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.CodeAnalysis;
5+
6+
namespace Microsoft.AspNetCore.Http.ValidationsGenerator;
7+
8+
public sealed partial class ValidationsGenerator : IIncrementalGenerator
9+
{
10+
public void Initialize(IncrementalGeneratorInitializationContext context)
11+
{
12+
return;
13+
}
14+
}

Diff for: src/Http/Http.Extensions/test/Microsoft.AspNetCore.Http.Extensions.Tests.csproj

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
55
<!-- This is needed to support codepaths that use NullabilityInfoContext. -->
6-
<Features>$(Features.Replace('nullablePublicOnly', '')</Features>
6+
<Features>$(Features.Replace('nullablePublicOnly', ''))</Features>
77
<PreserveCompilationContext>true</PreserveCompilationContext>
88
</PropertyGroup>
99

@@ -21,6 +21,8 @@
2121
<Reference Include="Microsoft.AspNetCore.TestHost" />
2222
<Reference Include="Microsoft.Extensions.DependencyInjection" />
2323
<Reference Include="Microsoft.Extensions.DependencyModel" />
24+
<Reference Include="Verify.XUnit" />
25+
<Reference Include="Verify.SourceGenerators" />
2426
</ItemGroup>
2527

2628
<ItemGroup>
@@ -29,7 +31,9 @@
2931
</ItemGroup>
3032

3133
<ItemGroup>
32-
<ProjectReference Include="..\gen\Microsoft.AspNetCore.Http.RequestDelegateGenerator.csproj" />
34+
<ProjectReference Include="..\gen\Microsoft.AspNetCore.Http.RequestDelegateGenerator\Microsoft.AspNetCore.Http.RequestDelegateGenerator.csproj" />
35+
<ProjectReference Include="..\gen\Microsoft.AspNetCore.Http.ValidationsGenerator\Microsoft.AspNetCore.Http.ValidationsGenerator.csproj" />
36+
3337
</ItemGroup>
3438

3539
<ItemGroup>
@@ -41,5 +45,6 @@
4145

4246
<ItemGroup>
4347
<HelixContent Include="$(MSBuildProjectDirectory)\RequestDelegateGenerator\Baselines\**" />
48+
<HelixContent Include="$(MSBuildProjectDirectory)\ValidationsGenerator\snapshots\**" />
4449
</ItemGroup>
4550
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Http.ValidationsGenerator.Tests;
5+
6+
public partial class ValidationsGeneratorTests : ValidationsGeneratorTestBase
7+
{
8+
[Fact]
9+
public async Task CanValidateParameters()
10+
{
11+
var source = """
12+
using System;
13+
using System.ComponentModel.DataAnnotations;
14+
using Microsoft.AspNetCore.Builder;
15+
using Microsoft.AspNetCore.Routing;
16+
17+
var builder = WebApplication.CreateBuilder();
18+
19+
var app = builder.Build();
20+
21+
app.MapGet("/params", (
22+
[Range(10, 100)] int value1,
23+
[Range(10, 100), Display(Name = "Valid identifier")] int value2,
24+
[Required] string value3 = "some-value",
25+
[CustomValidation(ErrorMessage = "Value must be an even number")] int value4 = 4,
26+
[CustomValidation, Range(10, 100)] int value5 = 10) => "OK");
27+
28+
app.Run();
29+
30+
public class CustomValidationAttribute : ValidationAttribute
31+
{
32+
public override bool IsValid(object? value) => value is int number && number % 2 == 0;
33+
}
34+
""";
35+
await Verify(source, out var compilation);
36+
VerifyEndpoint(compilation, "/params", async endpoint =>
37+
{
38+
var context = CreateHttpContext();
39+
context.Request.QueryString = new QueryString("?value1=5&value2=5&value3=&value4=3&value5=5");
40+
await endpoint.RequestDelegate(context);
41+
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
42+
});
43+
}
44+
}

0 commit comments

Comments
 (0)