Skip to content

Minimal APIs naming cleanup #35570

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 6 commits into from
Aug 21, 2021
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 @@ -8,14 +8,14 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

namespace Microsoft.AspNetCore.Analyzers.MinimalActions;
namespace Microsoft.AspNetCore.Analyzers.DelegateEndpoints;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public partial class MinimalActionAnalyzer : DiagnosticAnalyzer
public partial class DelegateEndpointAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(new[]
{
DiagnosticDescriptors.DoNotUseModelBindingAttributesOnMinimalActionParameters,
DiagnosticDescriptors.DoNotUseModelBindingAttributesOnDelegateEndpointParameters,
});

public override void Initialize(AnalysisContext context)
Expand All @@ -35,7 +35,7 @@ public override void Initialize(AnalysisContext context)
{
var invocation = (IInvocationOperation)operationAnalysisContext.Operation;
var targetMethod = invocation.TargetMethod;
if (IsMapActionInvocation(wellKnownTypes, invocation, targetMethod))
if (IsDelegateHandlerInvocation(wellKnownTypes, invocation, targetMethod))
{
return;
}
Expand All @@ -60,13 +60,13 @@ public override void Initialize(AnalysisContext context)
});
}

private static bool IsMapActionInvocation(
private static bool IsDelegateHandlerInvocation(
WellKnownTypes wellKnownTypes,
IInvocationOperation invocation,
IMethodSymbol targetMethod)
{
return !targetMethod.Name.StartsWith("Map", StringComparison.Ordinal) ||
!SymbolEqualityComparer.Default.Equals(wellKnownTypes.MinimalActionEndpointRouteBuilderExtensions, targetMethod.ContainingType) ||
!SymbolEqualityComparer.Default.Equals(wellKnownTypes.DelegateEndpointRouteBuilderExtensions, targetMethod.ContainingType) ||
invocation.Arguments.Length != 3;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

using Microsoft.CodeAnalysis;

namespace Microsoft.AspNetCore.Analyzers.MinimalActions
namespace Microsoft.AspNetCore.Analyzers.DelegateEndpoints
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("MicrosoftCodeAnalysisReleaseTracking", "RS2008:Enable analyzer release tracking")]
internal static class DiagnosticDescriptors
{
internal static readonly DiagnosticDescriptor DoNotUseModelBindingAttributesOnMinimalActionParameters = new(
internal static readonly DiagnosticDescriptor DoNotUseModelBindingAttributesOnDelegateEndpointParameters = new(
"ASP0003",
"Do not use model binding attributes with Map actions",
"{0} should not be specified for a {1} delegate parameter",
"Do not use model binding attributes with Map handlers",
"{0} should not be specified for a {1} Delegate parameter",
"Usage",
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

namespace Microsoft.AspNetCore.Analyzers.MinimalActions;
namespace Microsoft.AspNetCore.Analyzers.DelegateEndpoints;

public partial class MinimalActionAnalyzer : DiagnosticAnalyzer
public partial class DelegateEndpointAnalyzer : DiagnosticAnalyzer
{
private static void DisallowMvcBindArgumentsOnParameters(
in OperationAnalysisContext context,
Expand All @@ -33,7 +33,7 @@ private static void DisallowMvcBindArgumentsOnParameters(
var methodName = invocation.TargetMethod.Name;

context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.DoNotUseModelBindingAttributesOnMinimalActionParameters,
DiagnosticDescriptors.DoNotUseModelBindingAttributesOnDelegateEndpointParameters,
location,
modelBindingAttribute.AttributeClass.Name,
methodName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;

namespace Microsoft.AspNetCore.Analyzers.MinimalActions;
namespace Microsoft.AspNetCore.Analyzers.DelegateEndpoints;

internal sealed class WellKnownTypes
{
public static bool TryCreate(Compilation compilation, [NotNullWhen(true)] out WellKnownTypes? wellKnownTypes)
{
wellKnownTypes = default;
const string MinimalActionEndpointRouteBuilderExtensions = "Microsoft.AspNetCore.Builder.MinimalActionEndpointRouteBuilderExtensions";
if (compilation.GetTypeByMetadataName(MinimalActionEndpointRouteBuilderExtensions) is not { } minimalActionEndpointRouteBuilderExtensions)
const string DelegateEndpointRouteBuilderExtensions = "Microsoft.AspNetCore.Builder.DelegateEndpointRouteBuilderExtensions";
if (compilation.GetTypeByMetadataName(DelegateEndpointRouteBuilderExtensions) is not { } delegateEndpointRouteBuilderExtensions)
{
return false;
}
Expand All @@ -33,15 +33,15 @@ public static bool TryCreate(Compilation compilation, [NotNullWhen(true)] out We

wellKnownTypes = new WellKnownTypes
{
MinimalActionEndpointRouteBuilderExtensions = minimalActionEndpointRouteBuilderExtensions,
DelegateEndpointRouteBuilderExtensions = delegateEndpointRouteBuilderExtensions,
IBinderTypeProviderMetadata = ibinderTypeProviderMetadata,
BindAttribute = bindAttribute,
};

return true;
}

public ITypeSymbol MinimalActionEndpointRouteBuilderExtensions { get; private init; }
public ITypeSymbol DelegateEndpointRouteBuilderExtensions { get; private init; }
public INamedTypeSymbol IBinderTypeProviderMetadata { get; private init; }
public INamedTypeSymbol BindAttribute { get; private init; }
}
2 changes: 1 addition & 1 deletion src/Http/Http.Extensions/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.AppendList<T>(th
static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpRequest! request) -> Microsoft.AspNetCore.Http.Headers.RequestHeaders!
static Microsoft.AspNetCore.Http.HeaderDictionaryTypeExtensions.GetTypedHeaders(this Microsoft.AspNetCore.Http.HttpResponse! response) -> Microsoft.AspNetCore.Http.Headers.ResponseHeaders!
static Microsoft.AspNetCore.Http.HttpContextServerVariableExtensions.GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext! context, string! variableName) -> string?
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Delegate! action, Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions? options = null) -> Microsoft.AspNetCore.Http.RequestDelegateResult!
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Delegate! handler, Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions? options = null) -> Microsoft.AspNetCore.Http.RequestDelegateResult!
static Microsoft.AspNetCore.Http.RequestDelegateFactory.Create(System.Reflection.MethodInfo! methodInfo, System.Func<Microsoft.AspNetCore.Http.HttpContext!, object!>? targetFactory = null, Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions? options = null) -> Microsoft.AspNetCore.Http.RequestDelegateResult!
static Microsoft.AspNetCore.Http.ResponseExtensions.Clear(this Microsoft.AspNetCore.Http.HttpResponse! response) -> void
static Microsoft.AspNetCore.Http.ResponseExtensions.Redirect(this Microsoft.AspNetCore.Http.HttpResponse! response, string! location, bool permanent, bool preserveMethod) -> void
Expand Down
28 changes: 14 additions & 14 deletions src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ public static partial class RequestDelegateFactory
private static readonly AcceptsMetadata DefaultAcceptsMetadata = new(new[] { "application/json" });

/// <summary>
/// Creates a <see cref="RequestDelegate"/> implementation for <paramref name="action"/>.
/// Creates a <see cref="RequestDelegate"/> implementation for <paramref name="handler"/>.
/// </summary>
/// <param name="action">A request handler with any number of custom parameters that often produces a response with its return value.</param>
/// <param name="handler">A request handler with any number of custom parameters that often produces a response with its return value.</param>
/// <param name="options">The <see cref="RequestDelegateFactoryOptions"/> used to configure the behavior of the handler.</param>
/// <returns>The <see cref="RequestDelegateResult"/>.</returns>
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
public static RequestDelegateResult Create(Delegate action, RequestDelegateFactoryOptions? options = null)
public static RequestDelegateResult Create(Delegate handler, RequestDelegateFactoryOptions? options = null)
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
{
if (action is null)
if (handler is null)
{
throw new ArgumentNullException(nameof(action));
throw new ArgumentNullException(nameof(handler));
}

var targetExpression = action.Target switch
var targetExpression = handler.Target switch
{
object => Expression.Convert(TargetExpr, action.Target.GetType()),
object => Expression.Convert(TargetExpr, handler.Target.GetType()),
null => null,
};

Expand All @@ -92,9 +92,9 @@ public static RequestDelegateResult Create(Delegate action, RequestDelegateFacto
ServiceProviderIsService = options?.ServiceProvider?.GetService<IServiceProviderIsService>()
};

var targetableRequestDelegate = CreateTargetableRequestDelegate(action.Method, options, factoryContext, targetExpression);
var targetableRequestDelegate = CreateTargetableRequestDelegate(handler.Method, options, factoryContext, targetExpression);

return new RequestDelegateResult(httpContext => targetableRequestDelegate(action.Target, httpContext), factoryContext.Metadata);
return new RequestDelegateResult(httpContext => targetableRequestDelegate(handler.Target, httpContext), factoryContext.Metadata);

}

Expand Down Expand Up @@ -149,14 +149,14 @@ public static RequestDelegateResult Create(MethodInfo methodInfo, Func<HttpConte
// Task Invoke(HttpContext httpContext)
// {
// // Action parameters are bound from the request, services, etc... based on attribute and type information.
// return ExecuteTask(action(...), httpContext);
// return ExecuteTask(handler(...), httpContext);
// }

// void return type

// Task Invoke(HttpContext httpContext)
// {
// action(...);
// handler(...);
// return default;
// }

Expand Down Expand Up @@ -351,7 +351,7 @@ private static Expression CreateParamCheckingResponseWritingMethodCall(
// return Task.CompletedTask;
// } :
// {
// // Logic generated by AddResponseWritingToMethodCall() that calls action(param1_local, param2_local, ...)
// // Logic generated by AddResponseWritingToMethodCall() that calls handler(param1_local, param2_local, ...)
// };
// }

Expand Down Expand Up @@ -431,7 +431,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
methodCall,
HttpContextExpr);
}
// ExecuteTask<T>(action(..), httpContext);
// ExecuteTask<T>(handler(..), httpContext);
else if (typeArg == typeof(string))
{
return Expression.Call(
Expand Down Expand Up @@ -459,7 +459,7 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
methodCall,
HttpContextExpr);
}
// ExecuteTask<T>(action(..), httpContext);
// ExecuteTask<T>(handler(..), httpContext);
else if (typeArg == typeof(string))
{
return Expression.Call(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ public void BuildRequestDelegateThrowsArgumentNullExceptions()

var serviceProvider = new EmptyServiceProvider();

var exNullAction = Assert.Throws<ArgumentNullException>(() => RequestDelegateFactory.Create(action: null!));
var exNullAction = Assert.Throws<ArgumentNullException>(() => RequestDelegateFactory.Create(handler: null!));
var exNullMethodInfo1 = Assert.Throws<ArgumentNullException>(() => RequestDelegateFactory.Create(methodInfo: null!));

Assert.Equal("action", exNullAction.ParamName);
Assert.Equal("handler", exNullAction.ParamName);
Assert.Equal("methodInfo", exNullMethodInfo1.ParamName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ namespace Microsoft.AspNetCore.Builder
/// <summary>
/// Builds conventions that will be used for customization of MapAction <see cref="EndpointBuilder"/> instances.
/// </summary>
public sealed class MinimalActionEndpointConventionBuilder : IEndpointConventionBuilder
public sealed class DelegateEndpointConventionBuilder : IEndpointConventionBuilder
{
private readonly List<IEndpointConventionBuilder> _endpointConventionBuilders;

internal MinimalActionEndpointConventionBuilder(IEndpointConventionBuilder endpointConventionBuilder)
internal DelegateEndpointConventionBuilder(IEndpointConventionBuilder endpointConventionBuilder)
{
_endpointConventionBuilders = new List<IEndpointConventionBuilder>() { endpointConventionBuilder };
}

internal MinimalActionEndpointConventionBuilder(List<IEndpointConventionBuilder> endpointConventionBuilders)
internal DelegateEndpointConventionBuilder(List<IEndpointConventionBuilder> endpointConventionBuilders)
{
_endpointConventionBuilders = endpointConventionBuilders;
}
Expand Down
Loading