Skip to content

Populate ActionDescriptor EndpointMetadata #34065

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

Merged
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 @@ -151,8 +151,10 @@ private T[] GetOrderedMetadataSlow<T>() where T : class
/// </summary>
public struct Enumerator : IEnumerator<object?>
{
#pragma warning disable IDE0044
// Intentionally not readonly to prevent defensive struct copies
private readonly object[] _items;
private object[] _items;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pranavkm This got changed by #34215 but the comment suggests it shouldn't have. I put it back when resolving merge conflicts.

#pragma warning restore IDE0044
private int _index;

internal Enumerator(EndpointMetadataCollection collection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ private ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, string
AddSupportedRequestFormats(apiDescription.SupportedRequestFormats, hasJsonBody, routeEndpoint.Metadata);
AddSupportedResponseTypes(apiDescription.SupportedResponseTypes, methodInfo.ReturnType, routeEndpoint.Metadata);

AddActionDescriptorEndpointMetadata(apiDescription.ActionDescriptor, routeEndpoint.Metadata);

return apiDescription;
}

Expand Down Expand Up @@ -339,8 +341,20 @@ private static void AddResponseContentTypes(IList<ApiResponseFormat> apiResponse
}
}

private static void AddActionDescriptorEndpointMetadata(
ActionDescriptor actionDescriptor,
EndpointMetadataCollection endpointMetadata)
{
if (endpointMetadata.Count > 0)
{
// ActionDescriptor.EndpointMetadata is an empty array by
// default so need to add the metadata into a new list.
actionDescriptor.EndpointMetadata = new List<object>(endpointMetadata);
}
}

// The CompilerGeneratedAttribute doesn't always get added so we also check if the type name starts with "<"
// For example,w "<>c" is a "declaring" type the C# compiler will generate without the attribute for a top-level lambda
// For example, "<>c" is a "declaring" type the C# compiler will generate without the attribute for a top-level lambda
// REVIEW: Is there a better way to do this?
private static bool IsCompilerGenerated(Type type) =>
Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute)) || type.Name.StartsWith('<');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Threading;
Expand Down Expand Up @@ -342,6 +343,21 @@ public void AddsDisplayNameFromRouteEndpoint()
Assert.Equal("FOO", apiDescription.ActionDescriptor.DisplayName);
}

[Fact]
public void AddsMetadataFromRouteEndpoint()
{
var apiDescription = GetApiDescription([ApiExplorerSettings(IgnoreApi = true)]() => { });

Assert.NotEmpty(apiDescription.ActionDescriptor.EndpointMetadata);

var apiExplorerSettings = apiDescription.ActionDescriptor.EndpointMetadata
.OfType<ApiExplorerSettingsAttribute>()
.FirstOrDefault();

Assert.NotNull(apiExplorerSettings);
Assert.True(apiExplorerSettings.IgnoreApi);
}

private IList<ApiDescription> GetApiDescriptions(
Delegate action,
string pattern = null,
Expand Down Expand Up @@ -399,7 +415,7 @@ private class ServiceProviderIsService : IServiceProviderIsService
{
public bool IsService(Type serviceType) => serviceType == typeof(IInferredServiceInterface);
}

private class HostEnvironment : IHostEnvironment
{
public string EnvironmentName { get; set; }
Expand Down