Make controllers with ApiControllerAttribute visible in ApiExplorer
This commit is contained in:
parent
0989e60f73
commit
de2aef61ba
|
|
@ -57,6 +57,13 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
foreach (var controllerModel in context.Result.Controllers)
|
||||
{
|
||||
var isApiController = controllerModel.Attributes.OfType<IApiBehaviorMetadata>().Any();
|
||||
if (isApiController &&
|
||||
controllerModel.ApiExplorer.IsVisible == null)
|
||||
{
|
||||
// Enable ApiExplorer for the controller if it wasn't already explicitly configured.
|
||||
controllerModel.ApiExplorer.IsVisible = true;
|
||||
}
|
||||
|
||||
var controllerHasSelectorModel = controllerModel.Selectors.Any(s => s.AttributeRouteModel != null);
|
||||
|
||||
foreach (var actionModel in controllerModel.Actions)
|
||||
|
|
|
|||
|
|
@ -102,6 +102,47 @@ namespace Microsoft.AspNetCore.Mvc.Internal
|
|||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnProvidersExecuting_MakesControllerVisibleInApiExplorer_IfItIsAnnotatedWithAttribute()
|
||||
{
|
||||
// Arrange
|
||||
var context = GetContext(typeof(TestApiController));
|
||||
var options = new TestOptionsManager<ApiBehaviorOptions>(new ApiBehaviorOptions
|
||||
{
|
||||
SuppressModelStateInvalidFilter = true,
|
||||
});
|
||||
|
||||
var provider = GetProvider(options);
|
||||
|
||||
// Act
|
||||
provider.OnProvidersExecuting(context);
|
||||
|
||||
// Assert
|
||||
var controller = Assert.Single(context.Result.Controllers);
|
||||
Assert.True(controller.ApiExplorer.IsVisible);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnProvidersExecuting_DoesNotModifyVisibilityInApiExplorer_IfValueIsAlreadySet()
|
||||
{
|
||||
// Arrange
|
||||
var context = GetContext(typeof(TestApiController));
|
||||
context.Result.Controllers[0].ApiExplorer.IsVisible = false;
|
||||
var options = new TestOptionsManager<ApiBehaviorOptions>(new ApiBehaviorOptions
|
||||
{
|
||||
SuppressModelStateInvalidFilter = true,
|
||||
});
|
||||
|
||||
var provider = GetProvider(options);
|
||||
|
||||
// Act
|
||||
provider.OnProvidersExecuting(context);
|
||||
|
||||
// Assert
|
||||
var controller = Assert.Single(context.Result.Controllers);
|
||||
Assert.False(controller.ApiExplorer.IsVisible);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnProvidersExecuting_ThrowsIfControllerWithAttribute_HasActionsWithoutAttributeRouting()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue