Look at ApiConventionType attributes defined on base types
Fixes https://github.com/aspnet/AspNetCore/issues/4951
This commit is contained in:
parent
97d3a34a3d
commit
199e3f14a2
|
|
@ -160,7 +160,7 @@ namespace Microsoft.AspNetCore.Mvc.Api.Analyzers
|
||||||
|
|
||||||
internal static IReadOnlyList<ITypeSymbol> GetConventionTypes(in ApiControllerSymbolCache symbolCache, IMethodSymbol method)
|
internal static IReadOnlyList<ITypeSymbol> GetConventionTypes(in ApiControllerSymbolCache symbolCache, IMethodSymbol method)
|
||||||
{
|
{
|
||||||
var attributes = method.ContainingType.GetAttributes(symbolCache.ApiConventionTypeAttribute).ToArray();
|
var attributes = method.ContainingType.GetAttributes(symbolCache.ApiConventionTypeAttribute, inherit: true).ToArray();
|
||||||
if (attributes.Length == 0)
|
if (attributes.Length == 0)
|
||||||
{
|
{
|
||||||
attributes = method.ContainingAssembly.GetAttributes(symbolCache.ApiConventionTypeAttribute).ToArray();
|
attributes = method.ContainingAssembly.GetAttributes(symbolCache.ApiConventionTypeAttribute).ToArray();
|
||||||
|
|
|
||||||
|
|
@ -290,6 +290,50 @@ namespace Microsoft.AspNetCore.Mvc.Api.Analyzers
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetDeclaredResponseMetadata_ApiConventionTypeAttributeOnType_Works()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var type = typeof(GetDeclaredResponseMetadata_ApiConventionTypeAttributeOnType);
|
||||||
|
var compilation = await GetResponseMetadataCompilation();
|
||||||
|
var controller = compilation.GetTypeByMetadataName(type.FullName);
|
||||||
|
var method = (IMethodSymbol)controller.GetMembers().First();
|
||||||
|
var symbolCache = new ApiControllerSymbolCache(compilation);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = SymbolApiResponseMetadataProvider.GetDeclaredResponseMetadata(symbolCache, method);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
// We should expect 3 entries specified by DefaultApiConventions.Post
|
||||||
|
Assert.Collection(
|
||||||
|
result.OrderBy(r => r.StatusCode),
|
||||||
|
metadata => Assert.True(metadata.IsDefault),
|
||||||
|
metadata => Assert.Equal(201, metadata.StatusCode),
|
||||||
|
metadata => Assert.Equal(400, metadata.StatusCode));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetDeclaredResponseMetadata_ApiConventionTypeAttributeOnBaseType_Works()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var type = typeof(GetDeclaredResponseMetadata_ApiConventionTypeAttributeOnBaseType);
|
||||||
|
var compilation = await GetResponseMetadataCompilation();
|
||||||
|
var controller = compilation.GetTypeByMetadataName(type.FullName);
|
||||||
|
var method = (IMethodSymbol)controller.GetMembers().First();
|
||||||
|
var symbolCache = new ApiControllerSymbolCache(compilation);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = SymbolApiResponseMetadataProvider.GetDeclaredResponseMetadata(symbolCache, method);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
// We should expect 3 entries specified by DefaultApiConventions.Post
|
||||||
|
Assert.Collection(
|
||||||
|
result.OrderBy(r => r.StatusCode),
|
||||||
|
metadata => Assert.True(metadata.IsDefault),
|
||||||
|
metadata => Assert.Equal(201, metadata.StatusCode),
|
||||||
|
metadata => Assert.Equal(400, metadata.StatusCode));
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public Task GetStatusCode_ReturnsValueFromConstructor()
|
public Task GetStatusCode_ReturnsValueFromConstructor()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -90,4 +90,20 @@ namespace Microsoft.AspNetCore.Mvc.Api.Analyzers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[ApiConventionType(typeof(DefaultApiConventions))]
|
||||||
|
public class GetDeclaredResponseMetadata_ApiConventionTypeAttributeOnType : Controller
|
||||||
|
{
|
||||||
|
public IActionResult Post(object model) => null;
|
||||||
|
}
|
||||||
|
|
||||||
|
[ApiConventionType(typeof(DefaultApiConventions))]
|
||||||
|
public class GetDeclaredResponseMetadata_ApiConventionTypeAttributeOnBaseTypeBase
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GetDeclaredResponseMetadata_ApiConventionTypeAttributeOnBaseType : GetDeclaredResponseMetadata_ApiConventionTypeAttributeOnBaseTypeBase
|
||||||
|
{
|
||||||
|
public IActionResult Post(object model) => null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue