Skip to content
Draft
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 @@ -11,14 +11,15 @@ namespace ServiceComposer.AspNetCore.Endpoints.Tests
{
public class Get_with_2_handlers
{
[Route("sample")]
class TestGetIntegerHandler : ICompositionRequestsHandler
{
class Model
{
[FromRoute]public int id { get; set; }
}

[HttpGet("/sample/{id}")]
[HttpGet("{id}")]
public async Task Handle(HttpRequest request)
{
var model = await request.Bind<Model>();
Expand All @@ -27,9 +28,10 @@ public async Task Handle(HttpRequest request)
}
}

[Route("sample")]
class TestGetStringHandler : ICompositionRequestsHandler
{
[HttpGet("/sample/{id}")]
[HttpGet("{id}")]
public Task Handle(HttpRequest request)
{
var vm = request.GetComposedResponseModel();
Expand Down
33 changes: 27 additions & 6 deletions src/ServiceComposer.AspNetCore/EndpointsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ private static CompositionEndpointBuilder CreateCompositionEndpointBuilder(
};
builder.Metadata.Add(methodMetadata);

var attributes = componentsGroup.SelectMany(component => component.Method.GetCustomAttributes());
foreach (var attribute in attributes)
var methodAttributes = componentsGroup.SelectMany(component => component.Method.GetCustomAttributes());
var classAttributes = componentsGroup.SelectMany(component => component.ComponentType.GetCustomAttributes());
foreach (var attribute in methodAttributes.Concat(classAttributes))
{
builder.Metadata.Add(attribute);
}
Expand All @@ -229,20 +230,40 @@ private static CompositionEndpointBuilder CreateCompositionEndpointBuilder(
.Select<Type, (Type ComponentType, MethodInfo Method, string Template)>(componentType =>
{
var method = ExtractMethod(componentType);
var template = method.GetCustomAttribute<TAttribute>()?.Template.TrimStart('/');
if (template != null && useCaseInsensitiveRouteMatching)
var template = method.GetCustomAttribute<TAttribute>()?.Template;
if (template != null)
{
template = template.ToLowerInvariant();
template = PrefixWithRouteTemplateIfAny(componentType, template);
if (useCaseInsensitiveRouteMatching)
{
template = template.ToLowerInvariant();
}
}

return (componentType, method, template);
return (componentType, method, template?.TrimStart('/'));
})
.Where(component => component.Template != null)
.GroupBy(component => component.Template);

return getComponentsGroupedByTemplate;
}

static string PrefixWithRouteTemplateIfAny(Type componentType, string template)
{
if (template.StartsWith('/') || template.StartsWith("~/"))
{
return template;
}

var routeTemplate = componentType.GetCustomAttribute<RouteAttribute>()?.Template;
if (routeTemplate == null)
{
return template;
}

return string.Concat(routeTemplate, "/", template);
}

static MethodInfo ExtractMethod(Type componentType)
{
if (typeof(ICompositionRequestsHandler).IsAssignableFrom(componentType))
Expand Down