Handle null attributes in generated delegates (#7210)
This commit is contained in:
parent
b5d8fe9630
commit
f3072339de
|
|
@ -327,9 +327,15 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add delegate attributes as metadata
|
// Add delegate attributes as metadata
|
||||||
foreach (var attribute in requestDelegate.Method.GetCustomAttributes())
|
var attributes = requestDelegate.Method.GetCustomAttributes();
|
||||||
|
|
||||||
|
// This can be null if the delegate is a dynamic method or compiled from an expression tree
|
||||||
|
if (attributes != null)
|
||||||
{
|
{
|
||||||
routeEndpointBuilder.Metadata.Add(attribute);
|
foreach (var attribute in attributes)
|
||||||
|
{
|
||||||
|
routeEndpointBuilder.Metadata.Add(attribute);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metadata != null)
|
if (metadata != null)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq.Expressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Routing;
|
using Microsoft.AspNetCore.Routing;
|
||||||
|
|
@ -133,6 +134,25 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
Assert.IsType<Metadata>(endpointBuilder1.Metadata[2]);
|
Assert.IsType<Metadata>(endpointBuilder1.Metadata[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MapEndpoint_GeneratedDelegateWorks()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var builder = new DefaultEndpointRouteBuilder();
|
||||||
|
|
||||||
|
Expression<RequestDelegate> handler = context => Task.CompletedTask;
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var endpointBuilder = builder.Map(RoutePatternFactory.Parse("/"), "Display name!", handler.Compile(), new Metadata());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var endpointBuilder1 = GetRouteEndpointBuilder(builder);
|
||||||
|
Assert.Equal("Display name!", endpointBuilder1.DisplayName);
|
||||||
|
Assert.Equal("/", endpointBuilder1.RoutePattern.RawText);
|
||||||
|
Assert.Equal(1, endpointBuilder1.Metadata.Count);
|
||||||
|
Assert.IsType<Metadata>(endpointBuilder1.Metadata[0]);
|
||||||
|
}
|
||||||
|
|
||||||
[Attribute1]
|
[Attribute1]
|
||||||
[Attribute2]
|
[Attribute2]
|
||||||
private static Task Handle(HttpContext context) => Task.CompletedTask;
|
private static Task Handle(HttpContext context) => Task.CompletedTask;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue