parent
13281613a5
commit
a73d073eea
|
|
@ -25,7 +25,8 @@ namespace Microsoft.AspNetCore.Mvc.Api.Analyzers
|
||||||
return false;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,20 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Mvc.Internal;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc
|
namespace Microsoft.AspNetCore.Mvc
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates that a type and all derived types are used to serve HTTP API responses. The presence of
|
/// Indicates that a type and all derived types are used to serve HTTP API responses.
|
||||||
/// this attribute can be used to target conventions, filters and other behaviors based on the purpose
|
/// <para>
|
||||||
/// of the controller.
|
/// 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>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
|
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
|
||||||
public class ApiControllerAttribute : ControllerAttribute, IApiBehaviorMetadata
|
public class ApiControllerAttribute : ControllerAttribute, IApiBehaviorMetadata
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
using Microsoft.AspNetCore.Mvc.Core;
|
using Microsoft.AspNetCore.Mvc.Core;
|
||||||
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
||||||
using Microsoft.AspNetCore.Mvc.Internal;
|
using Microsoft.AspNetCore.Mvc.Internal;
|
||||||
|
|
@ -75,7 +76,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
|
||||||
{
|
{
|
||||||
foreach (var controller in context.Result.Controllers)
|
foreach (var controller in context.Result.Controllers)
|
||||||
{
|
{
|
||||||
if (!controller.Attributes.OfType<IApiBehaviorMetadata>().Any())
|
if (!IsApiController(controller))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -123,5 +124,17 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationModels
|
||||||
return false;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Analyzer.Testing;
|
using Microsoft.AspNetCore.Analyzer.Testing;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Api.Analyzers.TestFiles.ApiControllerFactsTest;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -110,9 +111,25 @@ namespace TestNamespace
|
||||||
Assert.True(result);
|
Assert.True(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<Compilation> GetCompilation()
|
[Fact]
|
||||||
|
public async Task IsApiControllerAction_ReturnsTrue_IfAttributeIsDeclaredOnAssembly()
|
||||||
{
|
{
|
||||||
var testSource = MvcTestSource.Read(GetType().Name, "TestFile");
|
// 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 project = DiagnosticProject.Create(GetType().Assembly, new[] { testSource.Source });
|
var project = DiagnosticProject.Create(GetType().Assembly, new[] { testSource.Source });
|
||||||
|
|
||||||
return project.GetCompilationAsync();
|
return project.GetCompilationAsync();
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue