Skip to content

Omit key services with derived attribute from OpenAPI definitions #62946

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

Closed
wants to merge 1 commit into from
Closed
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 => a.AttributeType.IsAssignableTo(typeof(IFromServiceMetadata)) || a.AttributeType.IsAssignableTo(typeof(FromKeyedServicesAttribute))) ||
parameterType == typeof(HttpContext) ||
parameterType == typeof(HttpRequest) ||
parameterType == typeof(HttpResponse) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
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 => a.AttributeType.IsAssignableTo(typeof(IFromServiceMetadata)) || a.AttributeType.IsAssignableTo(typeof(FromKeyedServicesAttribute))) ||
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 @@ -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);
Expand Down Expand Up @@ -1086,6 +1087,10 @@ private interface IInferredServiceInterface
{
}

private class CustomFromKeyedServicesAttribute(string key) : FromKeyedServicesAttribute("Custom" + key)
{
}

private record BindAsyncRecord(int Value)
{
public static ValueTask<BindAsyncRecord> BindAsync(HttpContext context, ParameterInfo parameter) =>
Expand Down
Loading