Skip to content

[OpenApi] Support derived FromKeyedServices types to ignore parameters #62926

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 1 commit into from
Jul 28, 2025
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 @@ -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 => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType) || typeof(FromKeyedServicesAttribute).IsAssignableFrom(a.AttributeType)) ||
parameterType == typeof(HttpContext) ||
parameterType == typeof(HttpRequest) ||
parameterType == typeof(HttpResponse) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
Expand Down Expand Up @@ -1521,6 +1520,20 @@ public void GetApiDescription_ParameterDescription_SourceFromServices()
Assert.Empty(description.ParameterDescriptions);
}

[Fact]
public void GetApiDescription_ParameterDescription_SourceFromDerivedServices()
{
// Arrange
var action = CreateActionDescriptor(nameof(AcceptsFormatters_DerivedServices));

// Act
var descriptions = GetApiDescriptions(action);

// Assert
var description = Assert.Single(descriptions);
Assert.Empty(description.ParameterDescriptions);
}

[Fact]
public void GetApiDescription_ParameterDescription_SourceFromCustomModelBinder()
{
Expand Down Expand Up @@ -2509,6 +2522,10 @@ private void AcceptsFormatters_Services([FromServices] ITestService tempDataProv
{
}

private void AcceptsFormatters_DerivedServices([CustomFromServices] ITestService tempDataProvider, [CustomFromKeyedServices("foo")] ITestService keyedTempDataProvider)
{
}

private void AcceptsProductChangeDTO(ProductChangeDTO dto)
{
}
Expand Down Expand Up @@ -2953,4 +2970,8 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
return base.ConvertFrom(context, culture, value);
}
}

private class CustomFromKeyedServicesAttribute(object key) : FromKeyedServicesAttribute(key);

private class CustomFromServicesAttribute : FromServicesAttribute;
}
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,9 @@ public void DoesNotAddFromServiceParameterAsService()
{
Assert.Empty(GetApiDescription((IInferredServiceInterface foo) => { }).ParameterDescriptions);
Assert.Empty(GetApiDescription(([FromServices] InferredServiceClass foo) => { }).ParameterDescriptions);
Assert.Empty(GetApiDescription(([CustomFromServices] 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);
Expand Down Expand Up @@ -1918,4 +1920,8 @@ private class TestServiceProvider : IServiceProvider
}

private class GenericClass<TType> { public required TType Value { get; set; } }

private class CustomFromKeyedServicesAttribute(object key) : FromKeyedServicesAttribute(key);

private class CustomFromServicesAttribute : FromServicesAttribute;
}
2 changes: 1 addition & 1 deletion src/OpenApi/src/Services/OpenApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private List<IOpenApiParameter> 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 => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType) || typeof(FromKeyedServicesAttribute).IsAssignableFrom(a.AttributeType)) ||
parameter.ParameterType == typeof(HttpContext) ||
parameter.ParameterType == typeof(HttpRequest) ||
parameter.ParameterType == typeof(HttpResponse) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,9 @@ public void DoesNotAddFromServiceParameterAsService()
{
Assert.Empty(GetOpenApiOperation((IInferredServiceInterface foo) => { }).Parameters);
Assert.Empty(GetOpenApiOperation(([FromServices] int foo) => { }).Parameters);
Assert.Empty(GetOpenApiOperation(([CustomFromServices] 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);
Expand Down Expand Up @@ -1144,4 +1146,8 @@ public override Endpoint Build()
throw new NotImplementedException();
}
}

private class CustomFromKeyedServicesAttribute(object key) : FromKeyedServicesAttribute(key);

private class CustomFromServicesAttribute : FromServicesAttribute;
}
Loading