Skip to content

Commit 34e3b75

Browse files
authored
Omit keyed services from OpenAPI definitions (#50717)
1 parent c862836 commit 34e3b75

File tree

5 files changed

+6
-3
lines changed

5 files changed

+6
-3
lines changed

src/Mvc/Mvc.ApiExplorer/src/EndpointMetadataApiDescriptionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private static ParameterDescriptor CreateParameterDescriptor(ParameterInfo param
276276
{
277277
return (BindingSource.FormFile, fromFormAttribute.Name ?? parameter.Name ?? string.Empty, false, parameter.ParameterType);
278278
}
279-
else if (parameter.CustomAttributes.Any(a => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType)) ||
279+
else if (parameter.CustomAttributes.Any(a => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType) || typeof(FromKeyedServicesAttribute) == a.AttributeType) ||
280280
parameter.ParameterType == typeof(HttpContext) ||
281281
parameter.ParameterType == typeof(HttpRequest) ||
282282
parameter.ParameterType == typeof(HttpResponse) ||

src/Mvc/Mvc.ApiExplorer/test/DefaultApiDescriptionProviderTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Microsoft.AspNetCore.Routing;
2424
using Microsoft.AspNetCore.Routing.Constraints;
2525
using Microsoft.AspNetCore.Routing.Template;
26+
using Microsoft.Extensions.DependencyInjection;
2627
using Microsoft.Extensions.Options;
2728
using Microsoft.Net.Http.Headers;
2829
using Moq;
@@ -2472,7 +2473,7 @@ private void AcceptsId_Header([FromHeader] int id)
24722473
{
24732474
}
24742475

2475-
private void AcceptsFormatters_Services([FromServices] ITestService tempDataProvider)
2476+
private void AcceptsFormatters_Services([FromServices] ITestService tempDataProvider, [FromKeyedServices("foo")] ITestService keyedTempDataProvider)
24762477
{
24772478
}
24782479

src/Mvc/Mvc.ApiExplorer/test/EndpointMetadataApiDescriptionProviderTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ public void DoesNotAddFromServiceParameterAsService()
422422
{
423423
Assert.Empty(GetApiDescription((IInferredServiceInterface foo) => { }).ParameterDescriptions);
424424
Assert.Empty(GetApiDescription(([FromServices] int foo) => { }).ParameterDescriptions);
425+
Assert.Empty(GetApiDescription(([FromKeyedServices("foo")] int foo) => { }).ParameterDescriptions);
425426
Assert.Empty(GetApiDescription((HttpContext context) => { }).ParameterDescriptions);
426427
Assert.Empty(GetApiDescription((HttpRequest request) => { }).ParameterDescriptions);
427428
Assert.Empty(GetApiDescription((HttpResponse response) => { }).ParameterDescriptions);

src/OpenApi/src/OpenApiGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ private List<OpenApiParameter> GetOpenApiParameters(MethodInfo methodInfo, Route
419419
{
420420
return (true, null, null);
421421
}
422-
else if (parameter.CustomAttributes.Any(a => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType)) ||
422+
else if (parameter.CustomAttributes.Any(a => typeof(IFromServiceMetadata).IsAssignableFrom(a.AttributeType) || typeof(FromKeyedServicesAttribute) == a.AttributeType) ||
423423
parameter.ParameterType == typeof(HttpContext) ||
424424
parameter.ParameterType == typeof(HttpRequest) ||
425425
parameter.ParameterType == typeof(HttpResponse) ||

src/OpenApi/test/OpenApiGeneratorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ public void DoesNotAddFromServiceParameterAsService()
401401
{
402402
Assert.Empty(GetOpenApiOperation((IInferredServiceInterface foo) => { }).Parameters);
403403
Assert.Empty(GetOpenApiOperation(([FromServices] int foo) => { }).Parameters);
404+
Assert.Empty(GetOpenApiOperation(([FromKeyedServices("foo")] int foo) => { }).Parameters);
404405
Assert.Empty(GetOpenApiOperation((HttpContext context) => { }).Parameters);
405406
Assert.Empty(GetOpenApiOperation((HttpRequest request) => { }).Parameters);
406407
Assert.Empty(GetOpenApiOperation((HttpResponse response) => { }).Parameters);

0 commit comments

Comments
 (0)