Skip to content

Commit f9121bc

Browse files
authored
Add namespace and assembly check before interception (#51243)
* Add namespace and assembly check before interception * Add test for extension method targeting IEndpointRouteBuilder
1 parent a2ce0b2 commit f9121bc

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/Http/Http.Extensions/gen/StaticRouteHandlerModel/InvocationOperationExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public static bool IsValidOperation(this IOperation? operation, WellKnownTypes w
2727
{
2828
invocationOperation = null;
2929
if (operation is IInvocationOperation targetOperation &&
30+
targetOperation.TargetMethod.ContainingNamespace is { Name: "Builder", ContainingNamespace: { Name: "AspNetCore", ContainingNamespace: { Name: "Microsoft", ContainingNamespace.IsGlobalNamespace: true } } } &&
31+
targetOperation.TargetMethod.ContainingAssembly.Name is "Microsoft.AspNetCore.Routing" &&
3032
targetOperation.TryGetRouteHandlerArgument(out var routeHandlerParameter) &&
3133
routeHandlerParameter is { Parameter.Type: {} delegateType } &&
3234
SymbolEqualityComparer.Default.Equals(delegateType, wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Delegate)))

src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,4 +556,75 @@ public async Task SupportsHandlersWithSameSignatureButDifferentParameterNamesFro
556556
Assert.Equal(new EventId(5, "ImplicitBodyNotProvided"), log2.EventId);
557557
Assert.Equal(@"Implicit body inferred for parameter ""todo1"" but no body was provided. Did you mean to use a Service instead?", log2.Message);
558558
}
559+
560+
[Fact]
561+
public async Task SkipsMapWithIncorrectNamespaceAndAssembly()
562+
{
563+
var source = """
564+
using System;
565+
using Microsoft.AspNetCore.Builder;
566+
using Microsoft.AspNetCore.Routing;
567+
568+
namespace TestApp
569+
{
570+
public static class TestMapActions
571+
{
572+
public static IEndpointRouteBuilder MapTestEndpoints(this IEndpointRouteBuilder app)
573+
{
574+
app.ServiceProvider.Map(1, (string test) => "Hello world!");
575+
app.ServiceProvider.MapPost(2, (string test) => "Hello world!");
576+
app.Map(3, (string test) => "Hello world!");
577+
app.MapPost(4, (string test) => "Hello world!");
578+
return app;
579+
}
580+
}
581+
582+
public static class EndpointRouteBuilderExtensions
583+
{
584+
public static IServiceProvider Map(this IServiceProvider app, int id, Delegate requestDelegate)
585+
{
586+
return app;
587+
}
588+
589+
public static IEndpointRouteBuilder Map(this IEndpointRouteBuilder app, int id, Delegate requestDelegate)
590+
{
591+
return app;
592+
}
593+
}
594+
}
595+
namespace Microsoft.AspNetCore.Builder
596+
{
597+
public static class EndpointRouteBuilderExtensions
598+
{
599+
public static IServiceProvider MapPost(this IServiceProvider app, int id, Delegate requestDelegate)
600+
{
601+
return app;
602+
}
603+
604+
public static IEndpointRouteBuilder MapPost(this IEndpointRouteBuilder app, int id, Delegate requestDelegate)
605+
{
606+
return app;
607+
}
608+
}
609+
}
610+
""";
611+
var project = CreateProject();
612+
project = project.AddDocument("TestMapActions.cs", SourceText.From(source, Encoding.UTF8)).Project;
613+
var compilation = await project.GetCompilationAsync();
614+
615+
var generator = new RequestDelegateGenerator.RequestDelegateGenerator().AsSourceGenerator();
616+
GeneratorDriver driver = CSharpGeneratorDriver.Create(generators: new[]
617+
{
618+
generator
619+
},
620+
driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true),
621+
parseOptions: ParseOptions);
622+
driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out var updatedCompilation,
623+
out var diagnostics);
624+
var generatorRunResult = driver.GetRunResult();
625+
626+
// Emits diagnostic and generates source for all endpoints
627+
var result = Assert.IsType<GeneratorRunResult>(Assert.Single(generatorRunResult.Results));
628+
Assert.Empty(GetStaticEndpoints(result, GeneratorSteps.EndpointModelStep));
629+
}
559630
}

0 commit comments

Comments
 (0)