From 3f93126eabb301e22e9590b675078dd78895cbee Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Wed, 15 Sep 2021 14:14:25 -0700 Subject: [PATCH] Revert setting endpoint name automatically (#36518) (#36548) --- .../DelegateEndpointRouteBuilderExtensions.cs | 3 - ...egateEndpointRouteBuilderExtensionsTest.cs | 98 ++----------------- 2 files changed, 9 insertions(+), 92 deletions(-) diff --git a/src/Http/Routing/src/Builder/DelegateEndpointRouteBuilderExtensions.cs b/src/Http/Routing/src/Builder/DelegateEndpointRouteBuilderExtensions.cs index c118bc0fb44..23d815c183c 100644 --- a/src/Http/Routing/src/Builder/DelegateEndpointRouteBuilderExtensions.cs +++ b/src/Http/Routing/src/Builder/DelegateEndpointRouteBuilderExtensions.cs @@ -153,9 +153,6 @@ namespace Microsoft.AspNetCore.Builder || !TypeHelper.IsCompilerGeneratedMethod(handler.Method)) { endpointName ??= handler.Method.Name; - - builder.Metadata.Add(new EndpointNameMetadata(endpointName)); - builder.Metadata.Add(new RouteNameMetadata(endpointName)); builder.DisplayName = $"{builder.DisplayName} => {endpointName}"; } diff --git a/src/Http/Routing/test/UnitTests/Builder/DelegateEndpointRouteBuilderExtensionsTest.cs b/src/Http/Routing/test/UnitTests/Builder/DelegateEndpointRouteBuilderExtensionsTest.cs index 84a31d7361d..0746a5dbf23 100644 --- a/src/Http/Routing/test/UnitTests/Builder/DelegateEndpointRouteBuilderExtensionsTest.cs +++ b/src/Http/Routing/test/UnitTests/Builder/DelegateEndpointRouteBuilderExtensionsTest.cs @@ -615,54 +615,24 @@ namespace Microsoft.AspNetCore.Builder } [Fact] - // This test scenario simulates methods defined in a top-level program - // which are compiler generated. We currently do some manually parsing leveraging - // code in Roslyn to support this scenario. More info at https://github.com/dotnet/roslyn/issues/55651. - public void MapMethod_DoesNotEndpointNameForInnerMethod() + public void WithTags_CanSetTagsForEndpoint() { - var name = "InnerGetString"; var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new EmptyServiceProvider())); - string InnerGetString() => "TestString"; - _ = builder.MapDelete("/", InnerGetString); + string GetString() => "Foo"; + _ = builder.MapDelete("/", GetString).WithTags("Some", "Test", "Tags"); var dataSource = GetBuilderEndpointDataSource(builder); // Trigger Endpoint build by calling getter. var endpoint = Assert.Single(dataSource.Endpoints); - var endpointName = endpoint.Metadata.GetMetadata(); - var routeName = endpoint.Metadata.GetMetadata(); - var routeEndpointBuilder = GetRouteEndpointBuilder(builder); - Assert.Equal(name, endpointName?.EndpointName); - Assert.Equal(name, routeName?.RouteName); - Assert.Equal("HTTP: DELETE / => InnerGetString", routeEndpointBuilder.DisplayName); + var tagsMetadata = endpoint.Metadata.GetMetadata(); + Assert.Equal(new[] { "Some", "Test", "Tags" }, tagsMetadata?.Tags); } [Fact] - public void MapMethod_DoesNotEndpointNameForInnerMethodWithTarget() + public void MapMethod_DoesNotEndpointNameForMethodGroupByDefault() { - var name = "InnerGetString"; - var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new EmptyServiceProvider())); - var testString = "TestString"; - string InnerGetString() => testString; - _ = builder.MapDelete("/", InnerGetString); - - var dataSource = GetBuilderEndpointDataSource(builder); - // Trigger Endpoint build by calling getter. - var endpoint = Assert.Single(dataSource.Endpoints); - - var endpointName = endpoint.Metadata.GetMetadata(); - var routeName = endpoint.Metadata.GetMetadata(); - var routeEndpointBuilder = GetRouteEndpointBuilder(builder); - Assert.Equal(name, endpointName?.EndpointName); - Assert.Equal(name, routeName?.RouteName); - Assert.Equal("HTTP: DELETE / => InnerGetString", routeEndpointBuilder.DisplayName); - } - - - [Fact] - public void MapMethod_SetsEndpointNameForMethodGroup() - { - var name = "GetString"; + string GetString() => "Foo"; var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new EmptyServiceProvider())); _ = builder.MapDelete("/", GetString); @@ -673,59 +643,9 @@ namespace Microsoft.AspNetCore.Builder var endpointName = endpoint.Metadata.GetMetadata(); var routeName = endpoint.Metadata.GetMetadata(); var routeEndpointBuilder = GetRouteEndpointBuilder(builder); - Assert.Equal(name, endpointName?.EndpointName); - Assert.Equal(name, routeName?.RouteName); - Assert.Equal("HTTP: DELETE / => GetString", routeEndpointBuilder.DisplayName); - } - - [Fact] - public void WithNameOverridesDefaultEndpointName() - { - var name = "SomeCustomName"; - var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new EmptyServiceProvider())); - _ = builder.MapDelete("/", GetString).WithName(name); - - var dataSource = GetBuilderEndpointDataSource(builder); - // Trigger Endpoint build by calling getter. - var endpoint = Assert.Single(dataSource.Endpoints); - - var endpointName = endpoint.Metadata.GetMetadata(); - var routeName = endpoint.Metadata.GetMetadata(); - var routeEndpointBuilder = GetRouteEndpointBuilder(builder); - Assert.Equal(name, endpointName?.EndpointName); - Assert.Equal(name, routeName?.RouteName); - // Will still use the original method name, not the custom endpoint name - Assert.Equal("HTTP: DELETE / => GetString", routeEndpointBuilder.DisplayName); - } - - private string GetString() => "TestString"; - - [Fact] - public void MapMethod_DoesNotSetEndpointNameForLambda() - { - var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new EmptyServiceProvider())); - _ = builder.MapDelete("/", () => { }); - - var dataSource = GetBuilderEndpointDataSource(builder); - // Trigger Endpoint build by calling getter. - var endpoint = Assert.Single(dataSource.Endpoints); - - var endpointName = endpoint.Metadata.GetMetadata(); Assert.Null(endpointName); - } - - [Fact] - public void WithTags_CanSetTagsForEndpoint() - { - var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new EmptyServiceProvider())); - _ = builder.MapDelete("/", GetString).WithTags("Some", "Test", "Tags"); - - var dataSource = GetBuilderEndpointDataSource(builder); - // Trigger Endpoint build by calling getter. - var endpoint = Assert.Single(dataSource.Endpoints); - - var tagsMetadata = endpoint.Metadata.GetMetadata(); - Assert.Equal(new[] { "Some", "Test", "Tags" }, tagsMetadata?.Tags); + Assert.Null(routeName); + Assert.Equal("HTTP: DELETE / => GetString", routeEndpointBuilder.DisplayName); } [Theory]