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

Commit

Permalink
Allow ApiControlelrAttribute to be applied to assemblies
Browse files Browse the repository at this point in the history
Fixes #7343
  • Loading branch information
pranavkm committed Sep 6, 2018
1 parent 55f75f8 commit 77e6af7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public static bool IsApiControllerAction(ApiControllerSymbolCache symbolCache, I
return false;
}

if (!method.ContainingType.HasAttribute(symbolCache.IApiBehaviorMetadata, inherit: true))
if (!method.ContainingType.HasAttribute(symbolCache.IApiBehaviorMetadata, inherit: true) &&
!method.ContainingAssembly.HasAttribute(symbolCache.IApiBehaviorMetadata))
{
return false;
}
Expand Down
14 changes: 9 additions & 5 deletions src/Microsoft.AspNetCore.Mvc.Core/ApiControllerAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Mvc.Internal;

namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// Indicates that a type and all derived types are used to serve HTTP API responses. The presence of
/// this attribute can be used to target conventions, filters and other behaviors based on the purpose
/// of the controller.
/// Indicates that a type and all derived types are used to serve HTTP API responses.
/// <para>
/// Controllers decorated with this attribute are configured with features and behavior targeted at improving the
/// developer experience for building APIs.
/// </para>
/// <para>
/// When decorated on an assembly, all controllers in the assembly will be treated as controllers with API behavior.
/// </para>
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ApiControllerAttribute : ControllerAttribute, IApiBehaviorMetadata
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.Infrastructure;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void OnProvidersExecuting(ApplicationModelProviderContext context)
{
foreach (var controllerModel in context.Result.Controllers)
{
if (!controllerModel.Attributes.OfType<IApiBehaviorMetadata>().Any())
if (!IsApiController(controllerModel))
{
continue;
}
Expand Down Expand Up @@ -103,5 +104,17 @@ bool IsAttributeRouted(IList<SelectorModel> selectorModel)
return false;
}
}

private static bool IsApiController(ControllerModel controller)
{
if (controller.Attributes.OfType<IApiBehaviorMetadata>().Any())
{
return true;
}

var controllerAssembly = controller.ControllerType.Assembly;
var assemblyAttributes = controllerAssembly.GetCustomAttributes();
return assemblyAttributes.OfType<IApiBehaviorMetadata>().Any();
}
}
}
21 changes: 19 additions & 2 deletions test/Mvc.Api.Analyzers.Test/ApiControllerFactsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Analyzer.Testing;
using Microsoft.AspNetCore.Mvc.Api.Analyzers.TestFiles.ApiControllerFactsTest;
using Microsoft.CodeAnalysis;
using Xunit;

Expand Down Expand Up @@ -110,9 +111,25 @@ public async Task IsApiControllerAction_ReturnsTrue_ForValidActionMethods()
Assert.True(result);
}

private Task<Compilation> GetCompilation()
[Fact]
public async Task IsApiControllerAction_ReturnsTrue_IfAttributeIsDeclaredOnAssembly()
{
// Arrange
var compilation = await GetCompilation(nameof(IsApiControllerAction_ReturnsTrue_IfAttributeIsDeclaredOnAssembly));
var symbolCache = new ApiControllerSymbolCache(compilation);
var type = compilation.GetTypeByMetadataName(typeof(IsApiControllerAction_ReturnsTrue_IfAttributeIsDeclaredOnAssemblyController).FullName);
var method = (IMethodSymbol)type.GetMembers(nameof(IsApiControllerAction_ReturnsTrue_IfAttributeIsDeclaredOnAssemblyController.Action)).First();

// Act
var result = ApiControllerFacts.IsApiControllerAction(symbolCache, method);

// Assert
Assert.True(result);
}

private Task<Compilation> GetCompilation(string testFile = "TestFile")
{
var testSource = MvcTestSource.Read(GetType().Name, "TestFile");
var testSource = MvcTestSource.Read(GetType().Name, testFile);
var project = DiagnosticProject.Create(GetType().Assembly, new[] { testSource.Source });

return project.GetCompilationAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Mvc;

[assembly: ApiController]

namespace Microsoft.AspNetCore.Mvc.Api.Analyzers.TestFiles.ApiControllerFactsTest
{
public class IsApiControllerAction_ReturnsTrue_IfAttributeIsDeclaredOnAssemblyController : ControllerBase
{
public IActionResult Action() => null;
}
}

0 comments on commit 77e6af7

Please sign in to comment.