Skip to content

Commit 704673f

Browse files
authored
Only support adding names from local functions
1 parent 9ff9267 commit 704673f

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/Http/Routing/src/Builder/MinimalActionEndpointRouteBuilderExtensions.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,14 @@ public static MinimalActionEndpointConventionBuilder Map(
185185
// Add MethodInfo as metadata to assist with OpenAPI generation for the endpoint.
186186
builder.Metadata.Add(action.Method);
187187

188-
// Methods defined in a top-level program are generated as statics so the delegate
189-
// target will be null. Inline lambdas are compiler generated properties so they can
190-
// be filtered that way.
191-
if (action.Target == null || !TypeHelper.IsCompilerGenerated(action.Method.Name))
188+
// We only add endpoint names for types that are not compiler generated since
189+
// compiler generated types are mangled by default. This logic can be changed once
190+
// https://github.com/dotnet/roslyn/issues/55651 is addressed. For now, this will
191+
// not set the endpoint name metadata for:
192+
// - Local functions
193+
// - Inline lambdas
194+
// - Static functions
195+
if (!TypeHelper.IsCompilerGenerated(action.Method.Name))
192196
{
193197
builder.Metadata.Add(new EndpointNameMetadata(action.Method.Name));
194198
}

src/Http/Routing/test/UnitTests/Builder/MinimalActionEndpointRouteBuilderExtensionsTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,9 @@ public void MapFallbackWithoutPath_BuildsEndpointWithLowestRouteOrder()
361361

362362
[Fact]
363363
// This test scenario simulates methods defined in a top-level program
364-
// which are compiler generated.
365-
public void MapMethod_SetsEndpointNameForInnerMethod()
364+
// which are compiler generated. This can be re-examined once
365+
// https://github.com/dotnet/roslyn/issues/55651 is addressed.
366+
public void MapMethod_DoesNotEndpointNameForInnerMethod()
366367
{
367368
var builder = new DefaultEndpointRouteBuilder(new ApplicationBuilder(new EmptyServiceProvdier()));
368369
string InnerGetString() => "TestString";
@@ -373,8 +374,7 @@ public void MapMethod_SetsEndpointNameForInnerMethod()
373374
var endpoint = Assert.Single(dataSource.Endpoints);
374375

375376
var endpointName = endpoint.Metadata.GetMetadata<IEndpointNameMetadata>();
376-
Assert.NotNull(endpointName);
377-
Assert.Equal("InnerGetString", endpointName?.EndpointName);
377+
Assert.Null(endpointName);
378378
}
379379

380380
[Fact]

0 commit comments

Comments
 (0)