diff --git a/src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs b/src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs index 2f25045d3787..6ac98b069fb2 100644 --- a/src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs +++ b/src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs @@ -274,7 +274,7 @@ private static ParameterDescriptor CreateParameterDescriptor(ParameterInfo param { return (BindingSource.FormFile, fromFormAttribute.Name ?? parameter.Name ?? string.Empty, false, parameterType); } - else if (parameter.ParameterInfo.CustomAttributes.Any(a => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType) || typeof(FromKeyedServicesAttribute) == a.AttributeType) || + else if (parameter.ParameterInfo.CustomAttributes.Any(a => a.AttributeType.IsAssignableTo(typeof(IFromServiceMetadata)) || a.AttributeType.IsAssignableTo(typeof(FromKeyedServicesAttribute))) || parameterType == typeof(HttpContext) || parameterType == typeof(HttpRequest) || parameterType == typeof(HttpResponse) || diff --git a/src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs b/src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs index 7de4d5eb5b89..736de8edf95f 100644 --- a/src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs +++ b/src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs @@ -796,6 +796,7 @@ public void DoesNotAddFromServiceParameterAsService() Assert.Empty(GetApiDescription((IInferredServiceInterface foo) => { }).ParameterDescriptions); Assert.Empty(GetApiDescription(([FromServices] InferredServiceClass foo) => { }).ParameterDescriptions); Assert.Empty(GetApiDescription(([FromKeyedServices("foo")] InferredServiceClass foo) => { }).ParameterDescriptions); + Assert.Empty(GetApiDescription(([CustomFromKeyedServices("foo")] InferredServiceClass foo) => { }).ParameterDescriptions); Assert.Empty(GetApiDescription((HttpContext context) => { }).ParameterDescriptions); Assert.Empty(GetApiDescription((HttpRequest request) => { }).ParameterDescriptions); Assert.Empty(GetApiDescription((HttpResponse response) => { }).ParameterDescriptions); @@ -1814,6 +1815,10 @@ private class InferredServiceClass : IInferredServiceInterface { } + private class CustomFromKeyedServicesAttribute(string key) : FromKeyedServicesAttribute("Custom" + key) + { + } + private class ServiceProviderIsService : IServiceProviderIsService { public bool IsService(Type serviceType) => serviceType == typeof(IInferredServiceInterface); diff --git a/src/OpenApi/src/Services/OpenApiGenerator.cs b/src/OpenApi/src/Services/OpenApiGenerator.cs index fd56a779dc88..a0c2118eac4a 100644 --- a/src/OpenApi/src/Services/OpenApiGenerator.cs +++ b/src/OpenApi/src/Services/OpenApiGenerator.cs @@ -424,7 +424,7 @@ private List GetOpenApiParameters(MethodInfo methodInfo, Rout { return (true, null, null); } - else if (parameter.CustomAttributes.Any(a => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType) || typeof(FromKeyedServicesAttribute) == a.AttributeType) || + else if (parameter.CustomAttributes.Any(a => a.AttributeType.IsAssignableTo(typeof(IFromServiceMetadata)) || a.AttributeType.IsAssignableTo(typeof(FromKeyedServicesAttribute))) || parameter.ParameterType == typeof(HttpContext) || parameter.ParameterType == typeof(HttpRequest) || parameter.ParameterType == typeof(HttpResponse) || diff --git a/src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiGeneratorTests.cs b/src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiGeneratorTests.cs index 3dbf13d3995f..72de97aa9b9d 100644 --- a/src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiGeneratorTests.cs +++ b/src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiGeneratorTests.cs @@ -414,6 +414,7 @@ public void DoesNotAddFromServiceParameterAsService() Assert.Empty(GetOpenApiOperation((IInferredServiceInterface foo) => { }).Parameters); Assert.Empty(GetOpenApiOperation(([FromServices] int foo) => { }).Parameters); Assert.Empty(GetOpenApiOperation(([FromKeyedServices("foo")] int foo) => { }).Parameters); + Assert.Empty(GetOpenApiOperation(([CustomFromKeyedServices("foo")] int foo) => { }).Parameters); Assert.Empty(GetOpenApiOperation((HttpContext context) => { }).Parameters); Assert.Empty(GetOpenApiOperation((HttpRequest request) => { }).Parameters); Assert.Empty(GetOpenApiOperation((HttpResponse response) => { }).Parameters); @@ -1086,6 +1087,10 @@ private interface IInferredServiceInterface { } + private class CustomFromKeyedServicesAttribute(string key) : FromKeyedServicesAttribute("Custom" + key) + { + } + private record BindAsyncRecord(int Value) { public static ValueTask BindAsync(HttpContext context, ParameterInfo parameter) =>