Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit de2aef6

Browse files
committed
Make controllers with ApiControllerAttribute visible in ApiExplorer
1 parent 0989e60 commit de2aef6

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/Microsoft.AspNetCore.Mvc.Core/Internal/ApiBehaviorApplicationModelProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ public void OnProvidersExecuting(ApplicationModelProviderContext context)
5757
foreach (var controllerModel in context.Result.Controllers)
5858
{
5959
var isApiController = controllerModel.Attributes.OfType<IApiBehaviorMetadata>().Any();
60+
if (isApiController &&
61+
controllerModel.ApiExplorer.IsVisible == null)
62+
{
63+
// Enable ApiExplorer for the controller if it wasn't already explicitly configured.
64+
controllerModel.ApiExplorer.IsVisible = true;
65+
}
66+
6067
var controllerHasSelectorModel = controllerModel.Selectors.Any(s => s.AttributeRouteModel != null);
6168

6269
foreach (var actionModel in controllerModel.Actions)

test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/ApiBehaviorApplicationModelProviderTest.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,47 @@ public void OnProvidersExecuting_SkipsAddingFilterToActionIfFeatureIsDisabledUsi
102102
});
103103
}
104104

105+
[Fact]
106+
public void OnProvidersExecuting_MakesControllerVisibleInApiExplorer_IfItIsAnnotatedWithAttribute()
107+
{
108+
// Arrange
109+
var context = GetContext(typeof(TestApiController));
110+
var options = new TestOptionsManager<ApiBehaviorOptions>(new ApiBehaviorOptions
111+
{
112+
SuppressModelStateInvalidFilter = true,
113+
});
114+
115+
var provider = GetProvider(options);
116+
117+
// Act
118+
provider.OnProvidersExecuting(context);
119+
120+
// Assert
121+
var controller = Assert.Single(context.Result.Controllers);
122+
Assert.True(controller.ApiExplorer.IsVisible);
123+
}
124+
125+
[Fact]
126+
public void OnProvidersExecuting_DoesNotModifyVisibilityInApiExplorer_IfValueIsAlreadySet()
127+
{
128+
// Arrange
129+
var context = GetContext(typeof(TestApiController));
130+
context.Result.Controllers[0].ApiExplorer.IsVisible = false;
131+
var options = new TestOptionsManager<ApiBehaviorOptions>(new ApiBehaviorOptions
132+
{
133+
SuppressModelStateInvalidFilter = true,
134+
});
135+
136+
var provider = GetProvider(options);
137+
138+
// Act
139+
provider.OnProvidersExecuting(context);
140+
141+
// Assert
142+
var controller = Assert.Single(context.Result.Controllers);
143+
Assert.False(controller.ApiExplorer.IsVisible);
144+
}
145+
105146
[Fact]
106147
public void OnProvidersExecuting_ThrowsIfControllerWithAttribute_HasActionsWithoutAttributeRouting()
107148
{

0 commit comments

Comments
 (0)