diff --git a/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/InvocationOperationExtensions.cs b/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/InvocationOperationExtensions.cs index 88af1ffae722..8b4e727be268 100644 --- a/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/InvocationOperationExtensions.cs +++ b/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/InvocationOperationExtensions.cs @@ -27,6 +27,8 @@ public static bool IsValidOperation(this IOperation? operation, WellKnownTypes w { invocationOperation = null; if (operation is IInvocationOperation targetOperation && + targetOperation.TargetMethod.ContainingNamespace is { Name: "Builder", ContainingNamespace: { Name: "AspNetCore", ContainingNamespace: { Name: "Microsoft", ContainingNamespace.IsGlobalNamespace: true } } } && + targetOperation.TargetMethod.ContainingAssembly.Name is "Microsoft.AspNetCore.Routing" && targetOperation.TryGetRouteHandlerArgument(out var routeHandlerParameter) && routeHandlerParameter is { Parameter.Type: {} delegateType } && SymbolEqualityComparer.Default.Equals(delegateType, wellKnownTypes.Get(WellKnownTypeData.WellKnownType.System_Delegate))) diff --git a/src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.cs b/src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.cs index d85f44b5ba33..2ea6ce459d5f 100644 --- a/src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.cs +++ b/src/Http/Http.Extensions/test/RequestDelegateGenerator/CompileTimeCreationTests.cs @@ -556,4 +556,75 @@ public async Task SupportsHandlersWithSameSignatureButDifferentParameterNamesFro Assert.Equal(new EventId(5, "ImplicitBodyNotProvided"), log2.EventId); Assert.Equal(@"Implicit body inferred for parameter ""todo1"" but no body was provided. Did you mean to use a Service instead?", log2.Message); } + + [Fact] + public async Task SkipsMapWithIncorrectNamespaceAndAssembly() + { + var source = """ +using System; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; + +namespace TestApp +{ + public static class TestMapActions + { + public static IEndpointRouteBuilder MapTestEndpoints(this IEndpointRouteBuilder app) + { + app.ServiceProvider.Map(1, (string test) => "Hello world!"); + app.ServiceProvider.MapPost(2, (string test) => "Hello world!"); + app.Map(3, (string test) => "Hello world!"); + app.MapPost(4, (string test) => "Hello world!"); + return app; + } + } + + public static class EndpointRouteBuilderExtensions + { + public static IServiceProvider Map(this IServiceProvider app, int id, Delegate requestDelegate) + { + return app; + } + + public static IEndpointRouteBuilder Map(this IEndpointRouteBuilder app, int id, Delegate requestDelegate) + { + return app; + } + } +} +namespace Microsoft.AspNetCore.Builder +{ + public static class EndpointRouteBuilderExtensions + { + public static IServiceProvider MapPost(this IServiceProvider app, int id, Delegate requestDelegate) + { + return app; + } + + public static IEndpointRouteBuilder MapPost(this IEndpointRouteBuilder app, int id, Delegate requestDelegate) + { + return app; + } + } +} +"""; + var project = CreateProject(); + project = project.AddDocument("TestMapActions.cs", SourceText.From(source, Encoding.UTF8)).Project; + var compilation = await project.GetCompilationAsync(); + + var generator = new RequestDelegateGenerator.RequestDelegateGenerator().AsSourceGenerator(); + GeneratorDriver driver = CSharpGeneratorDriver.Create(generators: new[] + { + generator + }, + driverOptions: new GeneratorDriverOptions(IncrementalGeneratorOutputKind.None, trackIncrementalGeneratorSteps: true), + parseOptions: ParseOptions); + driver = driver.RunGeneratorsAndUpdateCompilation(compilation, out var updatedCompilation, + out var diagnostics); + var generatorRunResult = driver.GetRunResult(); + + // Emits diagnostic and generates source for all endpoints + var result = Assert.IsType(Assert.Single(generatorRunResult.Results)); + Assert.Empty(GetStaticEndpoints(result, GeneratorSteps.EndpointModelStep)); + } }