Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/8.0] Add namespace and assembly check before interception #51276

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 } } } &&
amcasey marked this conversation as resolved.
Show resolved Hide resolved
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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GeneratorRunResult>(Assert.Single(generatorRunResult.Results));
Assert.Empty(GetStaticEndpoints(result, GeneratorSteps.EndpointModelStep));
}
}